0

我正在尝试从实体模型中的多个表中进行选择。但是我想选择两列,但它没有成功。我的 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 字段。还有其他方法可以执行此操作吗?

4

2 回答 2

1

目前尚不清楚您要达到的目标。但是您肯定会尝试对单个名称字符串应用where过滤器(谓词语法也不正确)。这是有条件地返回和属性Dept_Name的查询:Raised_ByIssued_To

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
            };
于 2013-01-31T21:27:46.427 回答
1

试试这个:

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
            };
于 2013-01-31T21:31:21.537 回答