1
public class Employee
{
   public int Id { get; set; } 
   public string Title { get; set;} 
   //....other fields....
   //......
   //public Topics Interest { get; set; }     
   public IList<Topics> Interests { get; set; }     
}


public class Topics
{
   public int Id { get; set; }  ;
   public string Name { get; set; }  ;
   //other fields
}

public static IQueryable<EmployeeObject> QueryableSQL()
{
    IQueryable<EmployeeObject> queryable = EmployeeRepository.GetAllEmployee(); 
}

我上面的数据结构有员工,其中有多个兴趣,每个兴趣都有多个主题

我的问题是:

我将如何搜索 Employee.Interests.Name ?

   //i need help construct the linq....
   //the below will not work and look for something in the `EmployeeObject` rather in `Interests`
   IList<EmployeeObject> _emps = QueryableSQL().Where(x => x.Name== "Chess").ToList();
4

2 回答 2

2

您可以Any在子集合上使用来查找匹配EmployeeObject的 s

IList<EmployeeObject> _emps = 
    QueryableSQL().Where(x => x.Interests
                               .Any(i => i.Name== "Chess"))
                  .ToList();
于 2013-11-12T22:06:22.833 回答
2

这取决于你想要什么。您是否想要他们的任何兴趣与给定值匹配的项目?

var query = QueryableSQL().Where(employee => 
    employee.Interests.Any(interest => interest.Name ==  "Chess"));

当您能够用英语解释时,您希望翻译成 LINQ 的查询会容易得多。

于 2013-11-12T22:06:46.593 回答