Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the difference of two LIST<object> in c# using lambda expression [duplicate]

Tags:

c#

linq

I have two list of type Link

Link
{
    Title;
    url;
}

I have two list(List lst1 and List lst2 of type Link Now I want those element which is not in lst1 but in lst2 How can I do that using lambda expression. I dont want to use for loop.

like image 652
user3138879 Avatar asked Apr 10 '15 12:04

user3138879


2 Answers

For reference comparison:

list2.Except(list1);

For value comparison you can use:

list2.Where(el2 => !list1.Any(el1 => el1.Title == el2.Title && el1.url == el2.url));
like image 183
Alex Sikilinda Avatar answered Nov 14 '22 17:11

Alex Sikilinda


In set operations what you are looking for is

a union minus the intersect

so (list1 union list2) except (list1 intersect list2)

check out this link for linq set operations https://msdn.microsoft.com/en-us/library/bb546153.aspx

like image 45
XenoPuTtSs Avatar answered Nov 14 '22 17:11

XenoPuTtSs