2012-10-30 52 views
3

大家好我有一個查詢使用Linq返回一組記錄,其中之一是國家ID,我也有一個所需的國家數組。有沒有通過國家陣列或者循環和看到如果ID是結果的方式,我想這樣做linq查詢中循環數組

results = from r in results 
where 
//jump to my c# array 
for(int x = 0;x < array.count; x++) 
{ 
r.countryId.ToString().Contains(array[x]) 
} 
select r 

感謝

回答

4

試試這個

var list = from r in results 
      where array.Contains(r.countryId.ToString()) 
      select r; 
+0

非常感謝 –

0

你沒有給數組類型,因此假設它是testclass1

class TestClass1 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 

    class Country 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 

    List<Country> countries = new List<Country>(); 
    TestClass1[] arrays = new TestClass1[30]; 

    countries.Where(x => arrays.Select(y => y.Id).Contains(x.Id)).ToList(); 

我不知道這是否是最好的辦法,但我認爲這將奏效。

UPDATE:沒有注意到陣列屬於國家類型。抱歉。

1

你可以加入收藏,但我認爲Yograj Gupta的答案可能是更好的答案。

var query = from a in results 
      join b in array 
      on a.CountryId equals b.CountryId 
      select a;