我正在尝试从实体模型中的多个表中进行选择。但是我想选择两列,但只是没有成功。我拥有的 LINQ 语句是:
var searchResult = from i in _imEntities.Issues
join dept in _imEntities.Departments
on i.Issued_to_dept equals dept.Dept_ID
where i.State == 1
select new {
i.ID_No,
i.Issue_Date,
Raised_By = dept.Dept_Name
.Where(i.Raised_by_Dept == dept.Dept_ID),
Issued_To = dept.Dept_Name
.Where(i.Issued_to_dept == dept.Dept_ID),
Details = i.Details
};
列名都是正确的,但我只是无法将dept_Names放入Raised_By和Issued_To字段中。有没有其他方法可以执行此操作?
试试这个:
var query = from i in _imEntities.Issues
join dept_r in _imEntities.Departments
on i.Issued_to_dept equals dept_r.Dept_ID
join dept_i in _imEntities.Departments
on i.Issued_to_dept equals dept_i.Dept_ID
where i.State == 1
select new {
i.ID_No,
i.Issue_Date,
Raised_By = dept_r.Dept_Name,
Issued_To = dept_i.Dept_Name,
Details = i.Details
};
目前尚不清楚您要实现的目标。但是您肯定尝试在单个名称字符串上应用过滤器(谓词语法也不正确)。下面是有条件地返回Raised_By
和Issued_To
属性中的Dept_Name
的查询:
var query = from i in _imEntities.Issues
join dept in _imEntities.Departments
on i.Issued_to_dept equals dept.Dept_ID
where i.State == 1
select new {
i.ID_No,
i.Issue_Date,
Raised_By = (i.Raised_by_Dept == dept.Dept_ID) ? dept.Dept_Name : null,
Issued_To = (i.Issued_to_dept == dept.Dept_ID) ? dept.Dept_Name : null,
Details = i.Details
};