LINQ IQueryable with List;输入参数

发布于 2024-12-12 20:09:34 字数 544 浏览 0 评论 0原文

如果我输入一个字符串作为参数,以下函数效果很好,但我想为其提供一个字符串列表,即不是为其提供一个项目的名称,例如“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;
}

The following function works great if I enter one string as a parameter, but I would like to supply it a list of strings, i.e. instead of supplying it the name of one project, e.g. "AccountTracker", I can supply it multiple project names, i.e. "AccountTracker", "GameX", "HR1". How can I convert my LINQ query to do so?

There is the following relationship 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-12-19 20:09:34

将项目作为字符串列表发送,您可以使用如下的 Contains 方法

[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;
}

send the projects as string list and you can use Contains method as below

[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;
}

or you can send one string with projects as "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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文