如何用Linq筛选数据库中Status为Inactive的记录?技术求助
实现加载Status为Inactive数据的Linq查询修改方案
你只需要在原Linq语句中添加Where筛选条件,过滤出Status列值为Inactive的记录即可,修改后的完整代码如下:
public IActionResult UrlDatasource([FromBody] DataManagerRequest dm) { IEnumerable DataSource = _context.Customer .Where(x => x.Status == "Inactive") // 添加筛选条件 .OrderByDescending(x => x.OrderNo) .ToList(); DataOperations operation = new DataOperations(); return dm.RequiresCounts ? Json(new { result = DataSource}) : Json(DataSource); }
关键修改说明
- 通过
Where(x => x.Status == "Inactive")指定筛选规则,只保留状态为Inactive的客户数据 - 保持原有的
OrderByDescending(x => x.OrderNo)降序排序逻辑(如果不需要排序可直接移除该方法)
原始数据
| Id | Customer | Status |
|---|---|---|
| 1 | Mary | Active |
| 2 | Jane | Active |
| 3 | Vicky | Inactive |
| 4 | Anna | Active |
预期输出数据
| Id | Customer | Status |
|---|---|---|
| 3 | Vicky | Inactive |
内容的提问来源于stack exchange,提问作者Ken John
相关产品推荐
相关产品推荐

