您好,我正在尝试通过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过滤结果集?即使记录存在,也不会返回任何记录。
答案 0 :(得分:0)
执行以下操作:
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;
}
答案 1 :(得分:0)
您是否需要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);
}
答案 2 :(得分:0)
请尝试这个。
var dept = dbContext.tblDepartments.Where(d => d.DepartmentID == _DeptID)
.Select(d => new Department
{
DepartmentID = d.DepartmentID,
DepartmentName = d.DepartmentName
});
}