You can use GroupBy
operator, because in fact your operation is grouping:
returnVehicles = modelVehicles.GroupBy(x => new { x.Year, x.Make, x.Model },
x => new Foo() {
Year = x.Year,
Make = x.Make,
Model = x.Model
},
(key, values) => values.First())
.ToList();
And if you implement Equals
and GetHashCode
for Foo
:
public class Foo
{
public string Year { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public override bool Equals(object obj)
{
Foo other = obj as Foo;
if (other == null)
return false;
return string.Equals(Year, other.Year) &&
string.Equals(Make, other.Make) &&
string.Equals(Model, other.Model);
}
public override int GetHashCode()
{
int hash = 13;
hash = (hash * 7) + Year.GetHashCode();
hash = (hash * 7) + Make.GetHashCode();
hash = (hash * 7) + Model.GetHashCode();
return hash;
}
}
this can be simplified:
returnVehicles = modelVehicles.GroupBy(x => new Foo() {
Year = x.Year,
Make = x.Make,
Model = x.Model
},
(key, values) => key)
.ToList();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…