Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c# - Using Contains() to check multiple object values exist in list before adding new list?

How do I check for multiple properties in object to see if it exists in list before adding new object to list? The Contains() doesn't work here, so what should work instead?

What I have here is a large list model and I want to port some data over to a new shorter list model having no duplication values, having unique values.

Thanks..

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
           }
       );
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...