0
我想使用Linq執行嵌套GroupBy,我無法讓它工作。我的代碼如下:嵌套GroupBy使用Linq
var summaryFile = new RemittanceCenterFilesSummaryListModel
{
RemittanceFilesSummary = remittanceCenterSummaryListModel.RemittanceBatchSummaryRecord.GroupBy(x => new { x.FileId, x.SourceFileName })
.Select(x => new RemitanceCenterFileSummarizedModel
{
FileId = x.Key.FileId,
SourceFileName = x.Key.SourceFileName,
Batches = x.ToList().GroupBy(b => new { b => b.BatchCode })
.Select(c => new RemittanceCenterBatchSummarizedModel
{
FileId = x.Key.FileId,
SourceFileName = x.Key.SourceFileName,
BatchCode = c.Key,
BatchType = c.Key,
DetailRecordCountAdc = x.Count(y => y.BillingSystemCode == BillingSystemCode.Adc),
DetailRecordCountNotAdc = x.Count(y => y.BillingSystemCode == BillingSystemCode.Exceed),
AmountAdc = x.Where(y => y.BillingSystemCode == BillingSystemCode.Adc).Sum(y => y.PaymentAmount),
AmountNotAdc = x.Where(y => y.BillingSystemCode == BillingSystemCode.Exceed).Sum(y => y.PaymentAmount),
AmountTotal = x.Sum(y => y.PaymentAmount),
});
ScannedBatchCount = x.Count(y => y.BatchType == "S"),
ScannedBatchAmount = x.Where(y => y.BatchType == "S").Sum(y => y.PaymentAmount),
NonScannedBatchCount = x.Count(y => y.BatchType != "S"),
NonScannedBatchAmount = x.Where(y => y.BatchType != "S").Sum(y => y.PaymentAmount),
}).ToList()
};
第一的GroupBy工作正常,但是當我嘗試GROUPBY批次場,我發現了以下生成錯誤:
錯誤76無效的匿名類型成員聲明。匿名類型成員必須聲明爲成員分配,簡單名稱或成員訪問權限。
錯誤在這裏強調:
Batches = x.ToList().GroupBy(b => new { b => b.BatchCode })
有什麼建議?
嘗試'.GroupBy(b => new {BatchCode = b.BatchCode})' – slawekwin