1

我正在编写两个 LINQ 查询,我在第二个查询中使用第一个查询的结果集。

但在某些情况下,当数据库表中没有数据时,我的第一个查询返回 null,因此我的第二个查询失败,wsdetails.location并导致异常。wsdetails.worklocationnull

例外:

你调用的对象是空的

我的代码是这样的:

        var wsdetails = (from assetTable in Repository.Asset
                         join userAsset in Repository.UserAsset on
                         assetTable.Asset_Id equals userAsset.Asset_Id
                         join subLocationTable in Repository.SubLocation on
                         assetTable.Sub_Location_Id equals subLocationTable.Sub_Location_ID
                         where userAsset.User_Id == userCode
                         && assetTable.Asset_TypeId == 1 && assetTable.Asset_SubType_Id == 1
                         select new { workstation = subLocationTable.Sub_Location_Name, location = assetTable.Location_Id }).FirstOrDefault();


            result = (from emp in this.Repository.Employee
                      join designation in this.Repository.Designation on
                      emp.DesignationId equals designation.Id
                      where emp.Code == userCode
                      select new EmployeeDetails
                      {                             
                          firstname = emp.FirstName,
                          lastname = emp.LastName,                              
                          designation = designation.Title,
                          LocationId = wsdetails.location,
                          WorkStationName = wsdetails.workstation
                      }).SingleOrDefault();

作为一种解决方法,我可以检查

if wsdetails == null

并更改我的第二个 LINQ 逻辑,但我相信有一些方法可以null像运算符一样处理 LINQ 本身的值??

但我试过这个,它对我不起作用。

有什么帮助吗?

4

3 回答 3

2

而不是“二元”运算符??,也许您应该使用旧的三元运算符? :,就像在

wsdetails != null ? wsdetails.location : null
于 2012-08-22T11:59:30.030 回答
2

问题是 EF 无法将null 合并运算符转换为 SQL。就我个人而言,我看不出在执行下一个查询之前用 if 语句检查结果有什么问题。但是,如果您不想这样做,那么因为您的结果总是一个查询,为什么不这样做:

var wsdetails = (from assetTable in Repository.Asset 
                 join userAsset in Repository.UserAsset on 
                 assetTable.Asset_Id equals userAsset.Asset_Id 
                 join subLocationTable in Repository.SubLocation on 
                 assetTable.Sub_Location_Id equals subLocationTable.Sub_Location_ID 
                 where userAsset.User_Id == userCode 
                 && assetTable.Asset_TypeId == 1 && assetTable.Asset_SubType_Id == 1 
                 select new { workstation = subLocationTable.Sub_Location_Name, location = assetTable.Location_Id }).FirstOrDefault();  

result = (from emp in this.Repository.Employee 
          join designation in this.Repository.Designation on 
          emp.DesignationId equals designation.Id 
          where emp.Code == userCode 
          select new EmployeeDetails 
          {                              
              firstname = emp.FirstName, 
              lastname = emp.LastName,                               
              designation = designation.Title
           }).SingleOrDefault();

result.LocationId = wsdetails != null ? wsdetails.location : "someDefaultValue";
result.WorkStationName = wsdetails != null ? wsdetails.workstation ?? "someDefaultValue"; 
于 2012-08-22T12:07:30.170 回答
1

尝试不评估第一个查询,并在第二个查询中使用它。这应该会产生一个 SQL 语句。

var wsdetails = (from assetTable in Repository.Asset
                         join userAsset in Repository.UserAsset on
                         assetTable.Asset_Id equals userAsset.Asset_Id
                         join subLocationTable in Repository.SubLocation on
                         assetTable.Sub_Location_Id equals subLocationTable.Sub_Location_ID
                         where userAsset.User_Id == userCode
                         && assetTable.Asset_TypeId == 1 && assetTable.Asset_SubType_Id == 1
                         select new { workstation = subLocationTable.Sub_Location_Name, location = assetTable.Location_Id });
// wsdetails is still an IEnumerable/IQueryable


        result = (from emp in this.Repository.Employee
                  join designation in this.Repository.Designation on
                  emp.DesignationId equals designation.Id
                  where emp.Code == userCode
                  select new EmployeeDetails
                  {                             
                      firstname = emp.FirstName,
                      lastname = emp.LastName,                              
                      designation = designation.Title,
                      LocationId = wsdetails.First().location,
                      WorkStationName = wsdetails.First().workstation
                  }).SingleOrDefault();
于 2012-08-22T12:02:27.980 回答