2017-02-10 24 views
-4

我试图创建一个对象,需要按BatchCode进行分组,并在某些字段上进行汇总。我正在尝试使用Groupby函数来完成此操作,但是我遇到了困难,我将不胜感激。如何创建具有正确总计和分组的对象

输入记录:
记录1:

BatchCode 1234 
BatchType Scanned 
Amount 10.00 
RecType Adc 

记录2:

BatchCode 1234 
BatchType Scanned 
Amount 5.00 
RecType NotAdc 

记录3:

BatchCode 2222 
BatchType NonScanned 
Amount 25.00 
RecType Adc 

记录4:

BatchCode 2222 
BatchType NonScanned 
Amount 30.01 
RecType NotAdc 

预期的输出对象:

"Batches": [ 
    { 
     "BatchCode": "1234", 
     "BatchType": "Scanned", 
     "DetailRecordCountAdc": 1, 
     "DetailRecordCountNotAdc": 1, 
     "DetailRecordCountTotal": 2, 
     "AmountAdc": 10.00, 
     "AmountNotAdc": 5.00, 
     "AmountTotal": 15.00 
    }, 
    { 
     "BatchCode": "2222", 
     "BatchType": "Nonscanned", 
     "DetailRecordCountAdc": 1, 
     "DetailRecordCountNotAdc": 1, 
     "DetailRecordCountTotal": 2, 
     "AmountAdc": 25.00, 
     "AmountNotAdc": 30.01, 
     "AmountTotal": 55.01 
    } 
    ]   
+5

你能分享你的代码和安排(一些实体)吗? – kat1330

回答

1

要做到这一点,我径自并提出了一些假设。我的主要假设是你的实体如何设置。

这是我如何设置它们:

public enum BatchType 
{ 
    Scanned = 1, 
    NonScanned = 2 
} 

public enum RecType 
{ 
    Adc = 1, 
    NotAdc = 2 
} 

public class Batch 
{ 
    public int BatchCode { get; set; } 
    public BatchType BatchType { get; set; } 
    public double Amount { get; set; } 
    public RecType RecType { get; set; } 
} 

public class BatchGroup 
{ 
    public int BatchCode { get; set; } 
    public BatchType BatchType { get; set; } 
    public int DetailRecordCountAdc { get; set; } 
    public int DetailRecordCountNotAdc { get; set; } 
    public int DetailRecordCountTotal => DetailRecordCountAdc + DetailRecordCountNotAdc; 
    public double AmountAdc { get; set; } 
    public double AmountNotAdc { get; set; } 
    public double AmountTotal => AmountAdc + AmountNotAdc; 
} 

有一次,我的类和这样的地方,我创建的每一个正确的值对象:

var list = new[] 
{ 
    new Batch 
    { 
     BatchCode = 1234, 
     BatchType = BatchType.Scanned, 
     Amount = 10.00, 
     RecType = RecType.Adc 
    }, 
    new Batch 
    { 
     BatchCode = 1234, 
     BatchType = BatchType.Scanned, 
     Amount = 5.00, 
     RecType = RecType.NotAdc, 
    }, 
    new Batch 
    { 
     BatchCode = 2222, 
     BatchType = BatchType.NonScanned, 
     Amount = 25.00, 
     RecType = RecType.Adc, 
    }, 
    new Batch 
    { 
     BatchCode = 2222, 
     BatchType = BatchType.NonScanned, 
     Amount = 30.01, 
     RecType = RecType.NotAdc, 
    } 
}; 

一切就绪我做了LINQ声明。

var result = list.GroupBy(x => new { x.BatchCode, x.BatchType }).Select(x => new BatchGroup 
{ 
    BatchCode = x.Key.BatchCode, 
    BatchType = x.Key.BatchType, 
    DetailRecordCountAdc = x.Count(y => y.RecType == RecType.Adc), 
    DetailRecordCountNotAdc = x.Count(y => y.RecType == RecType.NotAdc), 
    AmountAdc = x.Where(y => y.RecType == RecType.Adc).Sum(y => y.Amount), 
    AmountNotAdc = x.Where(y => y.RecType == RecType.NotAdc).Sum(y => y.Amount) 
}); 
+0

抱歉没有完整的细节。我已经用我的场景测试了这个解决方案,这对我很有用。谢谢。 – Jason