2016-07-22 36 views
0

即時通訊使用Linqpad來測試我的EF查詢子查詢和我似乎無法讓我的最終結果,包括根據不同的條件如何獲得多資金是

StorePaymentInvoices表示字段的總和一些額外的列表包含一個FK到CustomerStatementBatchPayments。因此,如果StorePaymentInvoices中存在相應的值,則需要對CustomerStatementBatchPayment.net字段進行求和。

獲得的款項實際上是一團糟。有什麼建議麼? 有時在一個陳述中難以做到的事情,最終可以通過多個步驟輕鬆完成。

var retval = (
      from a in CustomerStatementBatches 
      join b in CustomerStatementBatchPayments on a.ID equals b.CustomerStatementBatchID into grp1 
      from c in grp1    
      where a.CustomerStatementID == StatementId 
      group c by c.CustomerStatementBatchID into grp2 
      from e in grp2    
      select new { 
       StatementId = e.CustomerStatementBatch.CustomerStatementID, 
       BatchId = e.CustomerStatementBatchID, 
       Applied = CustomerStatementBatchPayments.Where(csbp => !StorePaymentInvoices.Select (pi => pi.CustomerStatementBatchPaymentID).ToList().Contains(e.ID)).Sum (csbp => csbp.Net) 
      } 
      ).ToList(); 
retval.Dump();  

[更新1]
這是香港專業教育學院做了獲得「有條件的」和值,我似乎得到正確的數字。它生成的結果SQL有點難看,但在1秒內執行<。

var retval1 = (
      from a in CustomerStatementBatches 
      join b in CustomerStatementBatchPayments on a.ID equals b.CustomerStatementBatchID into grp1 
      from c in grp1    
      where a.CustomerStatementID == StatementId 
      group c by new { a.CustomerStatementID, c.CustomerStatementBatchID} into grp2 
      from e in grp2.Distinct() 
      select new { 
       StatementId = e.CustomerStatementBatch.CustomerStatementID, 
       BatchId = e.CustomerStatementBatchID 
      } 
      ).ToList() 
      .Distinct() 
      .Select(a => new 
      { 
       StatementId = a.StatementId, 
       BatchId = a.BatchId, 
       AppliedTotal = (from b in CustomerStatementBatchPayments.Where(r => r.CustomerStatementBatchID == a.BatchId) 
           join c in StorePaymentInvoices on b.ID equals c.CustomerStatementBatchPaymentID                  
           group b by b.CustomerStatementBatchID into g1 
           from d in g1 
           select new{ Total = (decimal?)d.Net}).DefaultIfEmpty().Sum (at => (decimal?)at.Total) ?? 0.0m, 
       Unappliedtotal = (from b in CustomerStatementBatchPayments.Where(r => r.CustomerStatementBatchID == a.BatchId) 
           .Where(s => !StorePaymentInvoices.Any (pi => pi.CustomerStatementBatchPaymentID == s.ID)) 
           select new{ Total = (decimal?)b.Net}).DefaultIfEmpty().Sum (at => (decimal?)at.Total) ?? 0.0m         
      }) 
      .ToList(); 

回答

0

試試這個

from a in db.CustomerStatementBatches 
join b in db.CustomerStatementBatchPayments 
      //.Where(i => ...) 
      .GroupBy(i => i.CustomerStatementBatchesId) 
      .Select(i => new { 
           CustomerStatementBatchesId = i.Key, 
           SumOfPayments = i.Sum(t => t.Net) 
          } 
        ) 
      into tmp from b in tmp.DefaultIfEmpty() 
     on a.CustomerStatementBatchesId equals b.CustomerStatementBatchesId 
select new 
{ 
    StatementId = a.CustomerStatementId, 
    BatchId = a.CustomerStatementBatchId, 
    Applied = ((b == null) ? 0 : b.SumOfPayments) 
} 
+0

再次看到原來的職位。我試圖做這些記錄在StorePaymentInvoices沒有FK匹配的總和。換句話說,我需要做一個有條件的總和。使用TSQL,它將是一個使用「不存在」的子查詢。還需要在tsql中的「where exists」結果中使用第二個字段。希望這是有道理的。我要看看我是否可以把它分成更小的陳述,但我好奇,如果我能做到一個。 – bitshift

+0

寫這個條件到'哪裏clauase'?讓我知道它的工作 – NEER

+0

我添加了原始帖子的更新。我想我在很多悲傷之後得到了它,但我知道它會變得混亂。 – bitshift