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

関連する問題