在linq中找到两个列表的交集? [英] Find the intersection of two lists in linq?

查看:101
本文介绍了在linq中找到两个列表的交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有int A,B的列表.我喜欢在linq中执行以下步骤

I have list of int A,B. i like to do the following step in linq

list<int> c = new List<int>();

for (int i = 0; i < a.count; i++)
{
    for (int j = 0; j < b.count; j++)
    {
        if (a[i] == b[j])
        {
            c.add(a[i]);
        }
    }
}

如果其a和b为object,我需要以这种方式检查特定属性并添加列表(如果它等于我如何在linq中做到这一点?).

if its a and b is object , I need check particular properties like this manner and add list if it equals how can i do this in linq?

推荐答案

您可以使用Intersect方法:

var c = a.Intersect(b);

这将返回a和b中的所有值.但是,不会考虑该项目在列表中的位置.

This return all values both in a and b. However, position of the item in the list isn't taken into account.

这篇关于在linq中找到两个列表的交集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆