怎么做?
int[] mas={1,2,3,4,...,n}
public var method1(mas)
{
var d = from i in Object where i.number==mas[0] || i.number==mas[1] || i.number==mas[2]|| ... || i.number==mas[n] select i;
return d;
}
答案 0 :(得分:2)
你会想要做这样的事情
var d = From i in Object
From n in mas
Where n == i.Number
Select i;
return d;
实际上现在我想起来了,每次比赛都会给你一个i的清单。
你可能正在寻找更像
的东西 //create a list for the items that match the criteria
List<ObjectToGet> d = new List<ObjectToGet>;
//Loop over each item in your Object
foreach(ObjectToGet objectItem in Object){
//If the item contains any match add it to the list
if((From n in mas Where n == d.Number Select n).Any){
d.Add(objectItem);
}
}
return d;
可能有一种方法可以在纯LINQ中编写它,但这在概念上是你要做的事情
答案 1 :(得分:0)
使用HashSet将mas
包裹到constructor并使用Contains
将本文视为生产:Introducing HashSet (Kim Hamilton)
所以你会以这样的结局结束:
int[] mas={1,2,3,4,...,n};
var set = new HashSet<int>(mas); // or you can init set with proper value without array
public var method1(mas)
{
var d = from i in Object where set.Contains(i.number) select i;
return d;
}
当你的源阵列足够小时,纯LINQ解决方案也很好(@msarchet)。