2012-08-27 109 views
1

我有一個LINQ等效查詢

List<string>  

List<string> students; 
students.Add("123Rob"); 
students.Add("234Schulz"); 

Dictionary<string,string> 

Dictionary<string, string> courses = new Dictionary<string, string>(); 
courses .Add("Rob", "Chemistry"); 
courses .Add("Bob", "Math"); 
courses .Add("Holly", "Physics"); 
courses .Add("Schulz", "Botany"); 

我的目標現在是讓一個列表與價值觀 - {Chemistry,Botany}

換句話說,我試圖讓LINQ相當於

select value from [courses] 
where [courses].key in 
(
select [courses].key from courses,students where [students].id LIKE '%courses.key%' 
) 

應如何LINQ查詢被陷害?

+0

[枚舉期間的LINQ投射]可能的重複(http://stackoverflow.com/questions/12146296/linq-casting-during-enumeration) –

+0

@LB你錯了..這不是一個重複的,請刪除dv – GilliVilla

回答

5

這是正確的答案:

var values = from c in courses 
      where students.Any(s => s.IndexOf(c.Key, StringComparison.OrdinalIgnoreCase) >= 0) 
      select c.Value; 

測試我的機器上。 希望這可以幫助。

+0

你最好使用's.IndexOf(c.Key,StringComparison.OrdinalIgnoreCase)> = 0' - 使用ToLower()對於進行不區分大小寫的比較並不理想。 – BAF

+0

你是對的,我編輯了答案 –