LINQ等效查询

时间:2012-08-27 21:36:18

标签: c# linq list dictionary linq-to-objects

我有一个

 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查询?

1 个答案:

答案 0 :(得分:5)

这是正确答案:

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

在我的机器上测试过。 希望这可以提供帮助。

相关问题