c# - 如何使用 Linq 或其他方式从列表中删除匹配项

Cfl*_*lux 2 c#

我正在尝试从列表中删除匹配的项目,这似乎是一项非常简单的任务,但幸运的是,我无法弄清楚。

示例列表:

List<int> points1 = new List<int>
{
    1, 2, 3, 3
};
Run Code Online (Sandbox Code Playgroud)

我正在努力uniquePoints1成为1,2

我知道有.Distinct()但这会返回1,2,3,这不是我想要的。

我也尝试了以下方法,.Distinct()但我得到一条红线说Comparison made to the same variable, did you mean to compare to something else?

List<int> uniquePoints1 = points1.Where(x => x == x);
List<int> uniquePoints1 = points1.RemoveAll(x => x == x);
Run Code Online (Sandbox Code Playgroud)

任何帮助或方向表示赞赏。

Ruf*_*s L 5

您可以使用该GroupBy方法对项目进行分组,然后仅返回计数为 的组中的数字1

List<int> uniquePoints = points
    .GroupBy(x => x)              // Group all the numbers
    .Where(g => g.Count() == 1)   // Filter on only groups that have one item
    .Select(g => g.Key)           // Return the group's key (which is the number)
    .ToList();

// uniquePoints = { 1, 2 }
Run Code Online (Sandbox Code Playgroud)