簡體   English   中英

在LINQ中加入+ Group + Aggregate

[英]Join + Group + Aggregate in LINQ

我有以下查詢,我將其放入DataGrid

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();

我想這樣做的是里面添加東西acc中的select 例如,我想添加PaymentProvider = acc.PaymentProvider 我嘗試以我能想到的方式翻轉它,包括從組中獲取First()並嘗試從那里訪問它,但我似乎無法找到訪問它的方法。 我可能只是遺漏了一些非常簡單的東西,但我一直在尋找谷歌一段時間,我似乎無法找到一個類似的例子。 有人有想法嗎?

使用匿名類型擴展分組源: 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),
                        };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM