I am having a basic issue here with linq . Though this can be solved with repeated loops . I am trying to know how can this be done in linq .
I have two lists .
List<string> a = new List<string>{"a","b","c","d","e","f"};
List<string> b = new List<string> { "a", "b", "c", "x", "y", "z" };
I want to compare with list a and whichever element in b is found in a . I want to remove that element from b . In other words I want to remove {“a”,”b”,”c”} from b based on comparison from list a and want to contain only {“x”,”y”,”z”} in list b . Is there a single statement linq to solve this ?
(I’m sure this is a duplicate of another post just a few days ago, but I can’t find it…)
If you need a modification in place, you can use
List<T>.RemoveAll:Or more efficiently (if the lists are large):
Note that modifying a collection in place isn’t idiomatic in LINQ, which is why the above uses the .NET 2 list-specific method.
Or if you’re happy to change
bto refer to a new list instead, then you can use LINQ: