您有一个很大的列表,目标是检索下面的新列表
今天:
number color brand size tiresize
-----------------------------------------
1 blue d 5 6
2 blue d 5 6
3 red b 3 3
4 red b 3 3
etc....
目标:
number color brand size tiresize
-----------------------------------------
blue d 5 6
red b 3 3
目标是检索区分的列表并删除“数字”。
这个样本很小,实际上该列表大约有26个数据库。
我正在考虑使用distinct(),但它会考虑到所有数据,并且我不想考虑到“数字”列
当您检索新列表时,请求之前使用相同的类与区别列表。
public Car
{
public int number
public string color
public string brand
public string size
public string tiresize
}
谢谢!
答案 0 :(得分:1)
var cars = new List<Car>
{
new Car{number = 1, color="blue", brand="d", size = "5", tiresize = "6"},
new Car{number = 2, color="blue", brand="d", size = "5", tiresize = "6"},
new Car{number = 3, color="red", brand="b", size = "3", tiresize = "3"},
new Car{number = 4, color="red", brand="b", size = "3", tiresize = "3"},
};
var distinctCars = cars.GroupBy(x => new {x.color, x.brand, x.size, x.tiresize});
foreach (var car in distinctCars)
{
Console.WriteLine(car.Key.color + ": " + car.Key.brand + ": " + car.Key.size + ": " + car.Key.tiresize);
}
答案 1 :(得分:1)
一种方法,使用匿名类型,它具有内置Equals
+ GetHashCode
实现,可比较所有属性:
var distinctCars = cars
.Select(c => new {c.color, c.brand, c.size, c.tiresize})
.Distinct()
.Select(x => new Car {color = x.color, brand = x.brand, size = x.size, tiresize = x.tiresize})
.ToList(();
其他方式是
Equals
覆盖GetHashCode
+ Car
IEquatable<Car>
IEqualityComparer<Car>
Distinct
醇>