Difference between two lists

前端 未结 12 1120
无人共我
无人共我 2020-11-22 13:19

I Have two generic list filled with CustomsObjects.

I need to retrieve the difference between those two lists(Items who are in the first without the items in the sec

相关标签:
12条回答
  • 2020-11-22 13:34
            List<int> list1 = new List<int>();
            List<int> list2 = new List<int>();
            List<int> listDifference = new List<int>();
    
            foreach (var item1 in list1)
            {
                foreach (var item2 in list2)
                {
                    if (item1 != item2)
                        listDifference.Add(item1);
                }
            }
    
    0 讨论(0)
  • 2020-11-22 13:36
    var resultList = checklist.Where(p => myList.All(l => p.value != l.value)).ToList();
    
    0 讨论(0)
  • 2020-11-22 13:38

    You could do something like this:

    var result = customlist.Where(p => !otherlist.Any(l => p.someproperty == l.someproperty));
    
    0 讨论(0)
  • 2020-11-22 13:38
    var list3 = list1.Where(x => !list2.Any(z => z.Id == x.Id)).ToList();
    

    Note: list3 will contain the items or objects that are not in both lists. Note: Its ToList() not toList()

    0 讨论(0)
  • 2020-11-22 13:38

    bit late but here is working solution for me

     var myBaseProperty = (typeof(BaseClass)).GetProperties();//get base code properties
                        var allProperty = entity.GetProperties()[0].DeclaringType.GetProperties();//get derived class property plus base code as it is derived from it
                        var declaredClassProperties = allProperty.Where(x => !myBaseProperty.Any(l => l.Name == x.Name)).ToList();//get the difference
    

    In above mention code I am getting the properties difference between my base class and derived class list

    0 讨论(0)
  • 2020-11-22 13:40
    List<ObjectC> _list_DF_BW_ANB = new List<ObjectC>();    
    List<ObjectA> _listA = new List<ObjectA>();
    List<ObjectB> _listB = new List<ObjectB>();
    
    foreach (var itemB in _listB )
    {     
        var flat = 0;
        foreach(var itemA in _listA )
        {
            if(itemA.ProductId==itemB.ProductId)
            {
                flat = 1;
                break;
            }
        }
        if (flat == 0)
        {
            _list_DF_BW_ANB.Add(itemB);
        }
    }
    
    0 讨论(0)
提交回复
热议问题