Q: How to create object with correct totals and grouping I'm trying to create an object that needs to be grouped by BatchCode and totaled on certain fields. I'm trying to accomplish this using the Groupby function but I'm having difficulty, any help would be appreciated. 
Input records:
Record 1:    
BatchCode 1234  
BatchType Scanned  
Amount    10.00  
RecType   Adc

Record 2:  
BatchCode 1234  
BatchType Scanned  
Amount    5.00  
RecType   NotAdc

Record 3:  
BatchCode 2222
BatchType NonScanned  
Amount    25.00  
RecType   Adc

Record 4:  
BatchCode 2222
BatchType NonScanned  
Amount    30.01  
RecType   NotAdc

Expected output object:
    "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
    }
  ]        


A: To accomplish this I went ahead and made some assumptions. My primary assumptions where how your entities were setup.
Here is how I set them up:
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;
}

Once I had the classes and such in place I created each of the objects with the correct values:
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,
    }
};

With everything in place I made the LINQ statement.
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)
});

