返回不在其他表中的所有条目以及与其他表linq中的条件匹配的条目

时间:2015-02-02 15:20:14

标签: c# linq linq-to-entities

我有这个查询

IEnumerable<AssetNotCheckOutReportItem> aco = 
       (from  i in dbCon.nfoInventories 
        join a in dbCon.asset_status on i.asset_id equals a.asset_id
        join emp in dbCon.nfoEmployees on a.jv_id equals emp.jv_id                    
        where (a.checked_in != null)
        select new Reports.AssetNotCheckOutReportItem 
        {
              asset_id = a.asset_id,  
              inv_desc = i.inv_desc, 
              jvid=a.jv_id,  
              fullname = emp.fullname, 
              checked_out = a.checked_out, 
              checked_in = a.checked_in
        });

我还想要包含asset_status表中找不到的库存。这些条目没有员工信息,并且会为jv_id,fullname,checked_in和checked_out返回null。

我已经做了正确的SQL查询,返回了我想要的结果。

select * from nfoInventory as i
left outer join asset_status a on i.asset_id = a.asset_id
left outer join nfoEmployee as e on a.jv_id = e.jv_id
where i.asset_id not in (Select asset_id from asset_status where checked_in is null)

但是我仍然无法让Linq与实体合作

我试过了

IEnumerable<AssetNotCheckOutReportItem> aco = (
        from  i in dbCon.nfoInventories
        from asset in dbCon.asset_status.Where(a => a.asset_id == i.asset_id).DefaultIfEmpty()
        from emp in dbCon.nfoEmployees.Where(ne => ne.jv_id == asset.jv_id).DefaultIfEmpty()
        where (asset.checked_in != null)
        select new Reports.AssetNotCheckOutReportItem 
        {
              asset_id = i.asset_id,  
              inv_desc = i.inv_desc, 
              jvid=a.jv_id,  
              fullname = emp.fullname, 
              checked_out = a.checked_out, 
              checked_in = a.checked_in
        });

但是得到:

  

LINQ to Entities无法识别该方法   'System.Linq.IQueryable`1 [Lester.Model.asset_status] DefaultIfEmpty   [asset_status] [System.Linq.IQueryable`1 [Lester.Model.asset_status])”   方法,并且此方法无法转换为商店表达式。

我试过了

  IEnumerable<AssetNotCheckOutReportItem> aco = (
          from i in dbCon.nfoInventories
          join a in dbCon.asset_status on i.asset_id equals a.asset_id
          join emp in dbCon.nfoEmployees on a.jv_id equals emp.jv_id
          where (a.checked_in != null) || !(from c in dbCon.asset_status select c.asset_id).Contains(a.asset_id)
          select new Reports.AssetNotCheckOutReportItem 
        {
              asset_id = i.asset_id,  
              inv_desc = i.inv_desc, 
              jvid=a.jv_id,  
              fullname = emp.fullname, 
              checked_out = a.checked_out, 
              checked_in = a.checked_in
        });

并获得:

  

LINQ to Entities无法识别方法'布尔值   包含[字符串] [System.Linq.IQueryable`1 [System.String]   System.String)'方法,这个方法无法翻译成   商店表达。

我也试过

IEnumerable<AssetNotCheckOutReportItem> aco = (from i in dbCon.nfoInventories
        join a in dbCon.asset_status on i.asset_id equals a.asset_id
        join emp in dbCon.nfoEmployees on a.jv_id equals emp.jv_id
        where (a.checked_in != null) || (!dbCon.asset_status.Any(c => c.asset_id == i.asset_id))
        select new Reports.AssetNotCheckOutReportItem 
        {
              asset_id = i.asset_id,  
              inv_desc = i.inv_desc, 
              jvid=a.jv_id,  
              fullname = emp.fullname, 
              checked_out = a.checked_out, 
              checked_in = a.checked_in
        });

但是获得与原始查询相同的结果。只是checked_in的资产,而不是从未检出过的资产。

我该怎么做?感谢

1 个答案:

答案 0 :(得分:0)

我无法使用一个查询来解决这个问题。我终于做了2个查询并将结果联合起来。

IEnumerable<AssetNotCheckOutReportItem> returned = (from a in dbCon.asset_status
                                                   join emp in dbCon.nfoEmployees on a.jv_id equals emp.jv_id
                                                   join i in dbCon.nfoInventories on a.asset_id equals i.asset_id
                                                   where (a.checked_in != null)
                                                   select new Reports.AssetNotCheckOutReportItem { 
                                                         asset_id = a.asset_id, 
                                                         inv_desc = i.inv_desc, 
                                                         jvid = a.jv_id, 
                                                         fullname = emp.fullname, 
                                                         checked_out = a.checked_out,
                                                         checked_in = a.checked_in
                                                   });

IEnumerable<AssetNotCheckOutReportItem> notcheckedout = from i in dbCon.nfoInventories
                                                        where (!dbCon.asset_status.Any(c => c.asset_id == i.asset_id))
                                                        select new Reports.AssetNotCheckOutReportItem { 
                                                                          asset_id = i.asset_id, 
                                                                          inv_desc = i.inv_desc, 
                                                                          jvid = null, 
                                                                          fullname = null, 
                                                                          checked_out = null,
                                                                          checked_in = null
                                                        };

IEnumerable<AssetNotCheckOutReportItem> assets = returned.Union(notcheckedout).OrderBy(q => q.asset_id);