2

我有一个 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 对实体运行它时,我没有得到任何记录。

我究竟做错了什么?

4

1 回答 1

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();
于 2013-04-07T13:06:47.907 回答