Linq加入声明

时间:2013-01-31 21:21:24

标签: linq

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

2 个答案:

答案 0 :(得分:1)

目前尚不清楚你想要实现的目标。但你绝对试图在单个名称字符串上应用 where 过滤器(谓词语法也不正确)。以下是有条件地在Dept_NameRaised_By属性中返回Issued_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
            };

答案 1 :(得分: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
            };