2013-03-22 29 views
0
List<string>resultList=new List<string>(); 
List<string>allID=new List<string>(); 
allID.Add("a1"); 
allID.Add("a2"); 
allID.Add("a3"); 
allID.Add("a4"); 
allID.Add("a5"); 
allID.Add("a6"); 
List<string>selectedID=new List<string>(); 
selectedID.Add("1"); 
selectedID.Add("3"); 
selectedID.Add("5"); 
resultList=(from a in allID where a.Contains(**one of the ID form selectedID**) select a).ToList(); 

可以告訴我工作版本嗎?Linq條件「其中id =列表<Item> newList」如何實現

+0

喜(BRO/sis誰負反對票),如果你知道答案,這太容易了,可以給我的網址或鏈接或關鍵字搜索谷歌? – william 2013-03-22 02:50:47

+0

澄清:'a1 == 1'或不? – 2013-03-22 02:53:52

+0

你能提供'resultList'的預期結果嗎?你的描述有點模棱兩可。 – mellamokb 2013-03-22 02:57:18

回答

2

如果你想檢查是否每個a正是通過selectedID入口內匹配,使用方法:

(from a in allID where selectedID.Contains(a) select a).ToList() 

這不是你的示例代碼返回任何比賽。


如果你想檢查是否每串a包含selectedID任何項中的內容,使用方法:

(from a in allID 
where selectedID.Any(s => a.Contains(s)) 
select a).ToList() 

這將返回{ "a1", "a3", "a5" }

+0

是的,那是我需要的。謝謝。 – william 2013-03-22 03:01:49

1

我想你」重新嘗試做的是:

resultList = 
    (from a in allID 
    where selectedID.Any(s => ("a" + s) == a) 
    select a) 
    .ToList(); 

它將返回allID項目也在selectedID(與一些格式調整)。

1
resultList = allID.Where(x => selectedID.Select(s => "a" + s).Contains(x)) 
        .ToList<string>(); 
0

還有一個。 LINQ是酷:)

要檢查是否含有[ALLID任何條目的內容selectedID

resultList = allID.Where(all => selectedID.Any(select => all.Contains(select))).ToList(); 

resultList將包含{ 「A1」, 「A3」, 「A5」}