你能使用linq来查看两个IEnumerables数据是否包含任何公共条目?

Nib*_*Pig 4 .net c# linq ienumerable

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!");
}
Run Code Online (Sandbox Code Playgroud)

你能使用linq来查看两个IEnumerables数据是否包含任何公共条目?

Luk*_*keH 9

是的,您可以使用Intersect和执行此操作Any:

bool anyCommonEntries = a.Intersect(b).Any();
Run Code Online (Sandbox Code Playgroud)