Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use linq to see if two IEnumerables of data contain any common entries?

IEnumerable<fishbiscuits> a = GetFishBiscuits(0);
IEnumerable<fishbiscuits> b = GetFishBiscuits(1);

if ([any of the results in either list match])
{
 // Do something ie
 Console.WriteLine("I see both a and b love at least one of the same type of fish biscuit!");
}

Can you use linq to see if two IEnumerables of data contain any common entries?

like image 663
NibblyPig Avatar asked Dec 28 '22 00:12

NibblyPig


1 Answers

Yes, you can do this using Intersect and Any:

bool anyCommonEntries = a.Intersect(b).Any();
like image 87
LukeH Avatar answered Feb 01 '23 01:02

LukeH