Linq to实体在同一个表上使用嵌套查询进行查询

时间:2013-04-07 12:20:12

标签: c# linq entity-framework linq-to-entities

我有一个SQL查询:

SELECT distinct A.stock_value_site,
                A.BALANCE_QUANTITY,
                A.BALANCE_NOMINAL_VALUE, 
                A.BALANCE_INDEXED_VALUE,
                V.STOCK_VALUE_ORDER,
                U.BALANCE_QUANTITY  
FROM            svr A,   
                svt V,
                svu U
WHERE V.Code = 500 and  
      A.Id = U.ID (+) and
      A.Id = (SELECT    max(B.id) 
              FROM      svr B,
                        sts
              WHERE     A.stock_value_site = B.stock_value_site and 
                        B.Id = sts.ID(+) and
                        B.item = _item and 
                        B.date < _from_date and
                        B.Id IS NULL)

我尝试将此查询转换为Linq到实体:

var data = (from A in _context.svr
           join V in _context.svt on A.svtId equals V.Code
           join U in _context.svu on A.Id equals U.Id into groupA
           from gA in groupA.DefaultIfEmpty()
           where V.Code == _openBalanceRecordId &&
                 A.Id == (from B in _context.svr
                          join st in _context.sts on B.Id equals st.Id into groupB
                          from gB in groupB.DefaultIfEmpty()
                          where A.svsId == B.svsId &&
                                B.ItemId == _item &&
                                B.Date < _startDate.Date
                          select B.Id).Max()
           select new
           {
               A.stock_value_site,
               A.BALANCE_QUANTITY,
               A.BALANCE_NOMINAL_VALUE, 
               A.BALANCE_INDEXED_VALUE,
               V.STOCK_VALUE_ORDER,
               gA == null ? 0 : gA.BALANCE_QUANTITY
           }).Distinct().ToList();

当我在oracle中运行SQL查询时,我得到了33条记录。但是当我使用Linq运行实体时,我没有得到任何记录。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我相信你的原始查询仍有一些问题,如果我明白你要做什么,这应该会更好:

var results = 
    (from a in _context.svr
     from v in _context.svt.Where(t => t.Code == 500)
     join u in _context.svu on u.Id equals a.Id into gU
     from x in gU.DefaultIfEmpty()
     where a.Id == 
         (from b in _context.svr
          join s in _context.sts on b.Id equals s.Id
          where b.item = _item
          and b.date < _from_date
          and b.svsId == a.svsId
          select b.Id).Max()
     select new
     {
         a.stock_value_site,
         a.BALANCE_QUANTITY,
         a.BALANCE_NOMINAL_VALUE, 
         a.BALANCE_INDEXED_VALUE,
         v.STOCK_VALUE_ORDER,
         x == null ? 0 : x.BALANCE_QUANTITY
     }).Distinct().ToList();

或者可能这样:

var results = 
    (from a in _context.svr
     from v in _context.svt.Where(t => t.Code == 500)
     join u in _context.svu on u.Id equals a.Id into gU
     from x in gU.DefaultIfEmpty()
     let subQuery = 
         (from b in _context.svr
          join s in _context.sts on b.Id equals s.Id
          where b.item = _item
          and b.date < _from_date
          select new { b.Id, b.svsId })
     join y in subQuery on a.svsId equals y.svsId into gY
     where a.Id = y.Max(b => b.Id)
     select new
     {
         a.stock_value_site,
         a.BALANCE_QUANTITY,
         a.BALANCE_NOMINAL_VALUE, 
         a.BALANCE_INDEXED_VALUE,
         v.STOCK_VALUE_ORDER,
         x == null ? 0 : x.BALANCE_QUANTITY
     }).Distinct().ToList();