Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 能否使用linq查看两个IEnumerable数据是否包含任何公共条目? IEnumerable a=GetFishCookits(0); IEnumerable b=GetFishcookies(1); if([任一列表中的任何结果匹配]) { //做点什么 WriteLine(“我发现a和b都喜欢至少一种相同类型的鱼饼干!”); }_C#_.net_Linq_Ienumerable - Fatal编程技术网

C# 能否使用linq查看两个IEnumerable数据是否包含任何公共条目? IEnumerable a=GetFishCookits(0); IEnumerable b=GetFishcookies(1); if([任一列表中的任何结果匹配]) { //做点什么 WriteLine(“我发现a和b都喜欢至少一种相同类型的鱼饼干!”); }

C# 能否使用linq查看两个IEnumerable数据是否包含任何公共条目? IEnumerable a=GetFishCookits(0); IEnumerable b=GetFishcookies(1); if([任一列表中的任何结果匹配]) { //做点什么 WriteLine(“我发现a和b都喜欢至少一种相同类型的鱼饼干!”); },c#,.net,linq,ienumerable,C#,.net,Linq,Ienumerable,能否使用linq查看两个IEnumerable数据是否包含任何公共条目?是的,可以使用and执行此操作: 从101个Linq样本中- 用于Intersect 太好了,我在摆弄“contains”:)了解Intersect()使用所有对象上的Equals()方法;如果元素不是内置的值类型、字符串或IEquatable/IsStructuralEquatable实现,则运行时将求助于对象的默认引用相等性检查,这可能不是您想要的。@KeithS:True,但如果需要,您可以解决此问题,通过在对象本身

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

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


从101个Linq样本中-

用于Intersect


太好了,我在摆弄“contains”:)了解Intersect()使用所有对象上的Equals()方法;如果元素不是内置的值类型、字符串或IEquatable/IsStructuralEquatable实现,则运行时将求助于对象的默认引用相等性检查,这可能不是您想要的。@KeithS:True,但如果需要,您可以解决此问题,通过在对象本身上重写
Equals
/
GetHashCode
,或者通过将自定义
IEqualityComparer
传递给
Intersect
方法。非常正确。我只是想确保OP知道Intersect()本身在大多数情况下都不是一个灵丹妙药。我第一次犯了这个错误,所以感谢你指出了这个错误-然后我将代码更改为
getfishbickets(0)。选择(c=>c.uniqueID):)
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!");
}
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);
    }
}