Remove duplicates from list of object

后端 未结 5 1957
悲&欢浪女
悲&欢浪女 2021-01-20 11:52

I have MyObject with field: id, a, b, c, e, f and I have List with 500 000 items, now how can I remove all duplicate items with of the same value of the parameter a, c, f? <

5条回答
  •  一整个雨季
    2021-01-20 12:39

    Code from this link worked great for me. https://nishantrana.me/2014/08/14/remove-duplicate-objects-in-list-in-c/

    public class MyClass
    {
    public string ID { get; set; }
    public string Value { get; set; }
    
    }
    
    List myList = new List();
    var xrmOptionSet = new MyClass();
    xrmOptionSet.ID = "1";
    xrmOptionSet.Value = "100";
    var xrmOptionSet1 = new MyClass();
    xrmOptionSet1.ID = "2";
    xrmOptionSet1.Value = "200";
    var xrmOptionSet2 = new MyClass();
    xrmOptionSet2.ID = "1";
    xrmOptionSet2.Value = "100";
    myList.Add(xrmOptionSet);
    myList.Add(xrmOptionSet1);
    myList.Add(xrmOptionSet2);
    
    // here we are first grouping the result by label and then picking the first item from each group
    var myDistinctList = myList.GroupBy(i => i.ID)
    .Select(g => g.First()).ToList();
    

提交回复
热议问题