简体   繁体   English

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

[英]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? 你能使用linq来查看两个IEnumerables数据是否包含任何公共条目?

是的,您可以使用IntersectAny执行此操作:

bool anyCommonEntries = a.Intersect(b).Any();
public void Linq50()
{
    int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
    int[] numbersB = { 1, 3, 5, 7, 8 };

    var commonNumbers = numbersA.Intersect(numbersB);

    Console.WriteLine("Common numbers shared by both arrays:");
    foreach (var n in commonNumbers)
    {
        Console.WriteLine(n);
    }
}

From 101 Linq Samples - Intersect 从101 Linq样品 - 相交

Msdn documentation for Intersect Intersect的Msdn文档

Extension Methods Roundup: Intersect, Union, AsNullable and GroupEvery 扩展方法综述:Intersect,Union,AsNullable和GroupEvery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM