2013-07-21 89 views
1

我有以下查詢,這是我投入DataGrid加入+集團+聚合在LINQ

var balancePerAccount = from bal in data.balanceDetails 
         join acc in data.userPaymentDetails on bal.userName equals acc.userName 
         where bal.paymentID == 0 
         group bal by bal.userName into g 
         where g.Sum(p => p.rebateBeforeService) >= 20 
         select new 
         { 
          Username = g.Key, 
          Amount = g.Sum(p => p.rebateBeforeService), 
         }; 
dgPaymentsPending.DataSource = balancePerAccount; 
dgPaymentsPending.DataBind(); 

我想這樣做是對select裏面添加acc東西。例如,我想添加PaymentProvider = acc.PaymentProvider。我嘗試着用我想到的每一種方式來翻閱,包括從組中獲得First(),並試圖從那裏訪問它,但我似乎無法找到訪問它的方法。我可能會錯過一些非常簡單的事情,但我一直在尋找谷歌一段時間,我似乎無法找到一個可比較的例子。任何人有想法?

回答

3

使用匿名類型擴展分組來源:new { bal, acc }然後用First

var balancePerAccount = from bal in data.balanceDetails 
         join acc in data.userPaymentDetails on bal.userName equals acc.userName 
         where bal.paymentID == 0 
         group new { bal, acc } by bal.userName into g 
         where g.Sum(p => p.bal.rebateBeforeService) >= 20 
         select new 
         { 
          Username = g.Key, 
          PaymentProvider = g.FirstOrDefault().acc.PaymentProvider, 
          Amount = g.Sum(p => p.bal.rebateBeforeService), 
         }; 
+0

是的,這確實起作用。感謝您的快速回復和編輯:) –