在 ASP.net 中使用Where子句

问题描述 投票:0回答:1

我是 ASP.net 新手。 假设我有一个名为 tblEmployees 的表,如下

Id  Name        DeartmentId
1   Sara        1
2   John        1
3   Alex        2
4   Yank        3
5   Mike        4
6   Foster      2

现在我创建一个名为“部门列表”的列表。

List<int> DepartmentList = new List<int>();

让我们在此列表中添加两个值。

DepartmentList.Add(2);
DepartmentList.Add(3)

让我们从Sql获取数据

Var applicationDbContext = _context. tblEmployees;

问题:现在我需要添加一个 where 子句并获取 applicationDbContext 的结果,其中 DepartmentId 位于 DepartmentList 中。在这种情况下,ID 号为 3、4 和 6 的员工应该出现在列表中。

var applicationDbContext2 = applicationDbContext.Where(t => t.DepartmentId in DepartmentList);

但是applicationDbContext2的最终视图没有任何数据。

c# asp.net arraylist where-clause
1个回答
0
投票

通常是通过

Contains
完成的:

var filtered = _context. tblEmployees
    .Where(t => DepartmentList.Contains(t.DepartmentId))
    .ToList();
© www.soinside.com 2019 - 2024. All rights reserved.