LINQ query to find if items in a list are contained in another list
Posted
by
cjohns
on Stack Overflow
See other posts from Stack Overflow
or by cjohns
Published on 2012-09-29T21:21:32Z
Indexed on
2012/09/29
21:37 UTC
Read the original article
Hit count: 224
I have the following code:
List<string> test1 = new List<string> { "@bob.com", "@tom.com" };
List<string> test2 = new List<string> { "[email protected]", "[email protected]" };
I need to remove anyone in test2 that has @bob.com or @tom.com.
What I have tried is this:
bool bContained1 = test1.Contains(test2);
bool bContained2 = test2.Contains(test1);
bContained1 = false but bContained2 = true. I would prefer not to loop through each list but instead use a Linq query to retrieve the data. bContained1 is the same condition for the Linq query that I have created below:
List<string> test3 = test1.Where(w => !test2.Contains(w)).ToList();
The query above works on an exact match but not partial matches.
I have looked at other queries but I can find a close comparison to this with Linq. Any ideas or anywhere you can point me to would be a great help.
© Stack Overflow or respective owner