您好,我正在尝试按 ID 检索特定部门。这是我的代码public void GetDeptId(int _DeptID){var dept = dbContext.tblDepartments .Select(d => new Department { DepartmentID = d.DepartmentID, DepartmentName = d.DepartmentName }).Where(d => d.DepartmentID == _DeptID);}如何根据 _DeptID 过滤结果集?即使记录存在,这也不会返回任何记录。
3 回答
慕桂英3389331
TA贡献2036条经验 获得超8个赞
您需要 Select 子句吗?如果你只需要检索一个部门,你可以这样做:
public void GetDeptId(int _DeptID){ var depts = dbContext.tblDepartments .Where(d => d.DepartmentID == _DeptID);}
或者,如果它只是一个,这将是有意义的:
public void GetDeptId(int _DeptID){ var dept = dbContext.tblDepartments .First(d => d.DepartmentID == _DeptID);}
MYYA
TA贡献1868条经验 获得超4个赞
做这样的事情:
public ICollection<Department> GetDeptId(int _DeptID)
{
var dept = dbContext.tblDepartments
.Select(d => new Department
{
DepartmentID = e.DepartmentID,
DepartmentName = e.DepartmentName
}).Where(c => c.DepartmentID == _DeptID).ToList();
return dept;
}
至尊宝的传说
TA贡献1789条经验 获得超10个赞
请试试这个。
var dept = dbContext.tblDepartments.Where(d => d.DepartmentID == _DeptID)
.Select(d => new Department
{
DepartmentID = d.DepartmentID,
DepartmentName = d.DepartmentName
});
}
- 3 回答
- 0 关注
- 82 浏览
添加回答
举报
0/150
提交
取消