Return 所有不在 other table 中的条目和在 other table linq 中匹配条件的条目

Return all entries not in other table and entries that match condition in other table linq

我有这个问题

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 table 中找不到的库存。这些条目将没有员工信息,并且 return 对于 jv_id、全名、checked_in 和 checked_out.

为空

我进行了正确的 SQL 查询,return 是我想要的结果。

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 to Entities 一起工作

我试过了

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 does not recognize the method 'System.Linq.IQueryable`1[Lester.Model.asset_status] DefaultIfEmpty [asset_status][System.Linq.IQueryable`1[Lester.Model.asset_status])' method, and this method cannot be translated into a store expression.

我试过了

  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 does not recognize the method 'Boolean Contains[String][System.Linq.IQueryable`1[System.String], System.String)' method, and this method cannot be translated into a store expression.

我也试过了

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 的资产,而不是从未检出的资产。

我该怎么做?谢谢

我从来没有能够使用一个查询来完成这项工作。我终于做了 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);