2014-11-20 36 views
-1

如何在对象检查多个属性,看它是否在列表中存在添加新对象之前列出?将载有()不会在这里工作,所以我应该不是工作?使用载有()来检查在列表中存在多个对象的值添加新列表之前?

什么我这里是一个大名单的模型,我想某些端口上的数据不具有重复值的新的短名单模式,具有独特的价值。

谢谢..

public class Fee { 
    public string Year { get; set; } 
    public string Make { get; set; } 
    public string Model { get; set; } 
    public string Vin { get; set; } 
    public string Color { get; set; } 
} 
public class Foo { 
    public string Year { get; set; } 
    public string Make { get; set; } 
    public string Model { get; set; } 
} 

List<Fee> modelVehicles = new List<Fee>(); 
List<Foo> returnVehicles = new List<Foo>(); 

modelVehicles.Add(new Fee() { Year = "2001", Make = "Ford", Model = "Mustang", Vin = "111", Color = "Green" }); 
modelVehicles.Add(new Fee() { Year = "2001", Make = "Ford", Model = "Mustang", Vin = "222", Color = "Red" }); 
modelVehicles.Add(new Fee() { Year = "2001", Make = "Ford", Model = "Mustang", Vin = "333", Color = "Black" }); 

foreach(var v in modelVehicles) 
{ 
    if (returnVehicles.Contains(new Foo { Year = v.Year, Make = v.Make, Model = v.Model }) == false) 
    { 
     returnVehicles.Add(
      new Foo { 
       Year = v.Year, 
       Make = v.Make, 
       Model = v.Model 
      } 
     ); 
    } 
} 

回答

1

您可以使用GroupBy运营商,因为其实你的操作分组:

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(); 

而且如果实现EqualsGetHashCodeFoo

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; 
    } 
} 

这样可以被简化:

returnVehicles = modelVehicles.GroupBy(x => new Foo() { 
               Year = x.Year, 
               Make = x.Make, 
               Model = x.Model 
              }, 
             (key, values) => key) 
           .ToList(); 
+0

我想你的意思'新{x.Year,x.Make,x.Model}' – 2014-11-20 23:07:09

0

如果您只想获取不同项目的列表,请使用Enumerable.Distinct。你只需要创建一个equality comparer

class VehicleEqualityComparer: EqualityComparer<Foo> 
{ 
    public override int GetHashCode(Foo f) 
    { 
     // A reasonably decent hash code for this application. 
     return (f.Make.GetHashCode() << 16) 
       | (f.Model.GetHashCode() >> 16) 
      ^f.Year; 
    } 

    public bool Equals(Foo x, Foo y) 
    { 
     if (x == null && y == null) return true; 
     if (x == null || y == null) return false; 
     return x.Year == y.Year 
      && x.Make == y.Make 
      && x.Model == y.Model; 
    } 
} 

而获得不同的项目清单:

var returnVehicles = modelVehicles.Distinct(new VehicleEqualityComparer()).ToList(); 
相关问题