2012-08-27 76 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

你是对的,我编辑了答案 –