C# Linq语句从两个单独的集合中获取匹配项

C# Linq语句从两个单独的集合中获取匹配项,c#,linq,entity-framework,C#,Linq,Entity Framework,我有以下情况: List<int> idCollection = new List<int>(); idCollection.Add(1); idCollection.Add(2); 此时调用ToList()不是一个选项,因为我正在创建一个动态搜索,结果集可能非常大。您的查询必须是这样的 IQueryable<Foo> query = context.Where(p => idCollection.Any(i=>i==p.ID )); IQue

我有以下情况:

List<int> idCollection = new List<int>();
idCollection.Add(1);
idCollection.Add(2);

此时调用
ToList()
不是一个选项,因为我正在创建一个动态搜索,结果集可能非常大。

您的查询必须是这样的

IQueryable<Foo> query = context.Where(p => idCollection.Any(i=>i==p.ID ));
IQueryable query=context.Where(p=>idCollection.Any(i=>i==p.ID));

您可以使用
Contains

IQueryable<Foo> query = context.Where(p => idCollection.Contains(p.ID))
IQueryable query=context.Where(p=>idCollection.Contains(p.ID))
使用此

IQueryable<Foo> query = context.Where(p => idCollection.Contains(p.ID));
IQueryable query=context.Where(p=>idCollection.Contains(p.ID));
附加的 用于不选择idCollection元素

IQueryable<Foo> query = context.Where(p => !idCollection.Contains(p.ID));
IQueryable query=上下文。其中(p=>!idCollection.Contains(p.ID));
尝试使用

string[] collection1 = new string[] { "1", "7", "4" };
string[] collection2 = new string[] { "6", "1", "7" };

var resultSet = collection1.Intersect<string>(collection2);

foreach (string s in resultSet)
{
    Console.WriteLine(s);
}
string[]collection1=新字符串[]{“1”、“7”、“4”};
字符串[]集合2=新字符串[]{“6”、“1”、“7”};
var resultSet=collection1.Intersect(collection2);
foreach(结果集中的字符串s)
{
控制台。写入线(s);
}

完全符合要求!非常感谢。
string[] collection1 = new string[] { "1", "7", "4" };
string[] collection2 = new string[] { "6", "1", "7" };

var resultSet = collection1.Intersect<string>(collection2);

foreach (string s in resultSet)
{
    Console.WriteLine(s);
}