2017-02-15 13 views
1

我想確定對象集合中特定字段的不同計數。使用LINQ找到唯一的字段計數

private static RemittanceCenterBatchSummaryListModel SummarizeFields(RemittanceCenterSummaryListModel remittanceCenterSummaryListModel) 
    { 
     var result = remittanceCenterSummaryListModel.RemittanceBatchSummaryRecord.GroupBy(x => new{x.FileId, x.SourceFileName, x.BatchCode, x.BatchType}) 
      .Select(x => new RemittanceCenterBatchSummarizedModel() 
      { 
       FileId = x.Key.FileId, 
       SourceFileName = x.Key.SourceFileName, 
       BatchCode = x.Key.BatchCode, 
       BatchType = x.Key.BatchType, 
       DetailRecordCountAdc = x.Count(y => y.BillingSystemCode == BillingSystemCode.Adc), 
       DetailRecordCountNotAdc = x.Count(y => y.BillingSystemCode == BillingSystemCode.Exd), 
       AmountAdc = x.Where(y => y.BillingSystemCode == BillingSystemCode.Adc).Sum(y => y.PaymentAmount), 
       AmountNotAdc = x.Where(y => y.BillingSystemCode == BillingSystemCode.Exd).Sum(y => y.PaymentAmount), 
       UniqueFileCount = x.Select(y => x.Key.FileId).Distinct().Count() 
      }); 

     return CreateSummaryListModel(result); 
    } 

輸入實體:

public class RemittanceCenterSummaryListModel 
{ 

    public RemittanceCenterSummaryListModel() 
    { 
     this.RemittanceBatchSummaryRecord = new List<RemittanceBatchProcessingModel>(); 
    } 

    public List<RemittanceBatchProcessingModel> RemittanceBatchSummaryRecord { get; private set; } 
} 

public class RemittanceCenterBatchSummarizedModel 
{ 
    public string FileId { get; set; } 
    public string SourceFileName { get; set; } 
    public string BatchCode { get; set; } 
    public string BatchType { get; set; } 
    public int DetailRecordCountAdc { get; set; } 
    public int DetailRecordCountNotAdc { get; set; } 
    public int DetailRecordCountTotal { get; set; } 
    public decimal AmountAdc { get; set; } 
    public decimal AmountNotAdc { get; set; } 
    public decimal AmountTotal { get; set; } 
    public BillingSystemCode BillingSystemCode { get; set; } 
    public int UniqueFileCount { get; set; } 
} 

    private static RemittanceCenterBatchSummaryListModel CreateSummaryListModel(IEnumerable<RemittanceCenterBatchSummarizedModel> summaryModels) 
    { 
     var summaryModelList = new RemittanceCenterBatchSummaryListModel(); 

     foreach (var summaryRec in summaryModels) 
     { 
      var summaryModel = new RemittanceCenterBatchSummarizedModel 
      { 
       FileId = summaryRec.FileId, 
       SourceFileName = summaryRec.SourceFileName, 
       BatchCode = summaryRec.BatchCode, 
       BatchType = summaryRec.BatchType, 
       DetailRecordCountAdc = summaryRec.DetailRecordCountAdc, 
       DetailRecordCountNotAdc = summaryRec.DetailRecordCountNotAdc, 
       AmountAdc = summaryRec.AmountAdc, 
       AmountNotAdc = summaryRec.AmountNotAdc, 
       UniqueFileCount = summaryRec.UniqueFileCount 
      }; 

      summaryModelList.RemittanceBatchSummary.Add(summaryModel); 
     } 

     return summaryModelList; 
    } 

示例輸入記錄:

記錄1:

FileId: '123' 
    SourceFileName: 'test.file.txt' 
    BatchCode: 'aaa' 
    BatchType: 'scanned' 
    PaymentAmount: '50.00' 
    BillingSystemCode: 'Adc' 

記錄1:

FileId: '1234' 
    SourceFileName: 'test.file2.txt' 
    BatchCode: 'aab' 
    BatchType: 'scanned' 
    PaymentAmount: '52.00' 
    BillingSystemCode: 'Adc' 

ActualOuput爲UniqueFileCount現場:

UniqueFileCount = 1 

ExpectedOutput結果UniqueFileCount現場:

UniqueFileCount = 2 

我在做什麼錯?

+0

您添加的樣品具有不同FileIds所以返回的計數將在1 –

回答

1

聽起來好像你想要整個集合中的FileId的不同計數,而不是僅僅爲每個組,因爲FileId是你分組的其中一個域。如果是這樣的話,那麼你可以計算出數第一

int distinctFileIds = remittanceCenterSummaryListModel.RemittanceBatchSummaryRecor‌​d 
    .Select(x => x.FileId) 
    .Distinct() 
    .Count(); 

然後使用您的Linq查詢

UniqueFileCount = distinctFileIds 
+0

是的,這爲我工作。謝謝你的反饋。 – Jason