2017-02-22 65 views
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 }) 

有什么建议?

+0

尝试'.GroupBy(b => new {BatchCode = b.BatchCode})' – slawekwin

回答

2

你的意思是

Batches = x.ToList().GroupBy(b => b.BatchCode) 

没有必要,如果你想通过组只有一个属性,以创建一个匿名类型。


如果你需要一个匿名类型,语法将

Batches = x.ToList().GroupBy(b => new { b.BatchCode }) 

你使用其他运营商的λ(b => new {b => b.BatchCode})匿名类型里面,这是无效的。

+0

感谢您的回应! – Jason