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
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);
}
}
var resultList = checklist.Where(p => myList.All(l => p.value != l.value)).ToList();
You could do something like this:
var result = customlist.Where(p => !otherlist.Any(l => p.someproperty == l.someproperty));
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()
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
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);
}
}