Get the differences between 2 lists

前端 未结 5 456
你的背包
你的背包 2020-12-10 20:36

I have two lists (ListA and ListB), the type of these lists is the same PersonInfo, the Loginfield is a unique key.

5条回答
  •  甜味超标
    2020-12-10 21:12

    EDIT

    Try this soltuion for detail difference : Compare two objects and find the differences


    How to: Find the Set Difference Between Two Lists (LINQ)

    Enumerable.Except Method (IEnumerable, IEnumerable) -Produces the set difference of two sequences by using the default equality comparer to compare values.

           double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
            double[] numbers2 = { 2.2 };
    
            IEnumerable onlyInFirstSet = numbers1.Except(numbers2);
    

    or

    //newList will include all the common data between the 2 lists
    List newList = list1.Intersect(list2).ToList();
    
    
    //differences will be the data not found 
    List differences = list1.RemoveAll(a => newList.Contains(a));
    

    or

    outer join to get difference

    var compare1to2 = from a in 
               from b in driveList2.Where(b => b.property == a.property).DefaultIfEmpty()
                                  select a;
    

提交回复
热议问题