There is a disjunction operation, i.e. associations. I want to take only two unique values ​​from two lists, but it’s not quite clear how to do this. Looked Except , Intersect is not that.

Here, for clarity

 List<string> lst1 = new List<string>(){"1","2","3"}; List<string> lst2 = new List<string>(){"1","2","4"}; //var lstRez = {"3","4"}; Вот что должно получиться в итоге, т.е. символ 4 здесь уникален в обоих списках 

Here is the merge operation graphically.

enter image description here

  • The disjunction of sets is union, yes. As a result of the operation of combining two lists, you get a list of 1, 2, 3, 4 . In the attached picture is not a union, but a symmetric difference. When performing a symmetric difference operation, the result will be 3, 4 on your example, not only 4 is unique, but also 3. - iksuy
  • sorry all right The picture is just a sign of U - I thought this union - Rajab
  • First, subtract the first from the second, then the second from the first, and combine the two calls Except and Concat - Grundy
  • Specification: in the picture the sign U means the universal set - Pavel Mayorov

2 answers 2

A symmetric set difference is the union of the differences of two sets; accordingly, you can do this:

 List<string> lst1 = new List<string>(){"1","2","3"}; List<string> lst2 = new List<string>(){"1","2","4"}; List<string> result = lst1.Except(lst2).Union(lst2.Except(lst1)).ToList(); 

Also, the ISet interface has a method:

 void SymmetricExceptWith(IEnumerable<T> other) 

    Another way is to subtract the intersection from the union:

     List<string> lst1 = new List<string>() { "1", "2", "3" }; List<string> lst2 = new List<string>() { "1", "2", "4" }; var lstRes = lst1.Union(lst2).Except(lst1.Intersect(lst2));