开发者

How can I compare two sets of data in linq and return the common data?

开发者 https://www.devze.com 2023-01-02 06:40 出处:网络
I have two IList<string> a and b. I want to find out what strings are i开发者_如何学Pythonn both a and b using LINQ.Use Intersect:

I have two IList<string> a and b. I want to find out what strings are i开发者_如何学Pythonn both a and b using LINQ.


Use Intersect:

Produces the set intersection of two sequences.

a.Intersect(b)

Example usage:

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);
}

Output:

bar
baz
0

精彩评论

暂无评论...
验证码 换一张
取 消