2017-02-10 54 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