Skip to main content
 首页 » 编程设计

c#之如何查询子对象

2024年02月13日1352php

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(); 

请您参考如下方法:

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

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

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