2010-10-26 29 views
-2

如何做到這一點?LINQ請求

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; 
} 
+1

怎麼辦呢? – 2010-10-26 14:10:40

+1

這不是最好寫的問題,但他很明顯正在嘗試做什麼。 – msarchet 2010-10-26 14:17:25

回答

2

你會想要做這樣的事情

var d = From i in Object 
     From n in mas 
     Where n == i.Number 
     Select i; 
return d; 

其實現在我想到的是,那是要回報給你我的列表每一場比賽。

你可能正在尋找更多的東西一樣

//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寫這樣的方式,但這個概念是什麼,你正在嘗試做的

0

使用​​包裝你masHashSet和使用Contains

檢查本文爲inroduction:

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; 
} 

Introducing HashSet (Kim Hamilton)

所以你用類似的東西結束當你有足夠小的源代碼時,純粹的LINQ解決方案也是很好的(@msarchet) rray。