c# - Comparing two lists with LINQ, and retrieve the different items -
assume have following object foo
, properties listed below:
class foo public string name public string property1 public string property2
now let's instantiate 2 lists of foo
: list<foo>()
the first list contains 2 foo
objects:
new foo() = { name = "name", property1 = "random value", property1 = "random value" } new foo() = { name = "name", property1 = "random value", property1 = "random value" }
the second list contains 1 foo
object:
new foo() = { name = "name", property1 = "random value", property1 = "random value" }
is possible using linq compare , return new list<foo>
contains differences between these 2 lists (or other list matter)? taking consideration item order , any property value not match considered difference.
this helpful primitive objects: compare 2 lists, item item, using linq not provide way return new list differences.
edit
@csharpie reopened... question looks poorly defined - i.e. expected result {1,2,3,4} , {1,3,4}? possibly op looking sort of diff..
@alexeilevenkov output should 2. or list of {1,2,3,4} , {1,3,2, 4} output should {3,2} since order (or values) of 3 , 2 have changed original list.
try using except linq method:
list<int> = new list<int>{ 1, 2, 5 }; list<int> b = new list<int>{ 2, 3}; var result = a.except(b).union(b.except(a)).tolist();
in set theory, (a-b)+(b-a)
equivalent differences between 2 of them.
Comments
Post a Comment