How to compare two datasets in linq and return common data?

I have two IList<string> a and b. I want to find out which lines are in a and b using LINQ.

+4
source share
1 answer

Use Intersect :

Produces many intersections of two sequences.

 a.Intersect(b) 

Usage example:

 IList<string> a = new List<string> { "foo", "bar", "baz" }; IList<string> b = new List<string> { "baz", "bar", "qux" }; var stringsInBoth = a.Intersect(b); foreach (string s in stringsInBoth) { Console.WriteLine(s); } 

Conclusion:

 bar baz 
+9
source

Source: https://habr.com/ru/post/1312035/


All Articles