Thursday, 6 August 2015

Use of Except operator in Linq

How to calculate subtraction (set operation) of ordered lists in C#?
E.g. after execution
of code:
List<int> a = new int[] { 1, 2, 5, 6, 7}.ToList();
List<int> b = new int[] { 1, 2, 3, 6}.ToList();
List<int> c = ListSubtract(a, b);
c should contains 5, 7.
It would be good to perform in O(a.Count()+b.Count()) operations, but it is not critical.

Solution use of Except in Linq Operator
 
List<int> c = a.Except(b).ToList();

1 comment: