LINQ IQueryable与List <string>输入参数</string>

时间:2011-10-30 06:43:01

标签: c# linq wcf-data-services

如果我输入一个字符串作为参数,以下函数效果很好,但我想提供一个字符串列表,即不提供一个项目的名称,例如“AccountTracker”,我可以提供多个项目名称,即“AccountTracker”,“GameX”,“HR1”。如何转换我的LINQ查询呢?

有以下关系Employee(1 - )EmployeeProjects( - 1)Project

[WebGet]
public IQueryable<Employee> GetEmployeesByProjects(string project)
{
   var employees = from ep in CurrentDataSource.EmployeeProjects
          .Include("Project")
          .Include("Employee")
       where ep.Project.ProjectName == project
       select ep.Employee;
   return employees;
}

1 个答案:

答案 0 :(得分:2)

将项目作为字符串列表发送,您可以使用包含方法,如下所示

[WebGet]
public IQueryable<Employee> GetEmployeesByProjects(List<string> projects)
{
   var employees = from ep in CurrentDataSource.EmployeeProjects
          .Include("Project")
          .Include("Employee")
       where projects.Contains(ep.Project.ProjectName)
       select ep.Employee;
   return employees;
}

或者你可以发送一个包含项目的字符串"AccountTracker,GameX,HR1"

[WebGet]
public IQueryable<Employee> GetEmployeesByProjects(string listofprojects)
{
   string[] projects= listofprojects.Split(',');
   var employees = from ep in CurrentDataSource.EmployeeProjects
          .Include("Project")
          .Include("Employee")
       where projects.Contains(ep.Project.ProjectName)
       select ep.Employee;
   return employees;
}