簡體   English   中英

如何使用Linq創建包含集合的唯一集合

[英]How to use Linq to create unique Collection which contains a collection

我正在從數據庫中檢索記錄並創建以下對象:

       public class RemittanceBatchProcessingModel
      {
            public string FileId { get; set; }
            public string SourceFileName { get; set; }
            public string BatchCode { get; set; }
            public string BatchType { get; set; }
            public decimal PaymentAmount { get; set; }
            public string BillingSystemCode { get; set; }
       }

db讀取后創建的示例對象:

FileId | SourceFileName | BatchCode | BatchType | PaymentAmt |BillingCode
    1  | test.file1.txt | 100       | S         | 1000.00    | Exc
    1  | test.file1.txt | 100       | S         | 2000.00    | Exc 
    1  | test.file1.txt | 200       | N         |  500.00    | Adc 
    2  | test.file2.txt | 300       | S         | 1200.00    | Exc 
    2  | test.file2.txt | 300       | S         | 1500.00    | Exc 

我想創建一個具有唯一文件集合的對象,該文件包含文件中每個匯總批處理的集合。 例如,

  Collection of Unique Files:
FileId | SourceFileName | BatchCode | BatchType | BatchTotal |RecordCount
    1  | test.file1.txt | 100       | S         | 3000.00    | 2
    1  | test.file1.txt | 200       | N         |  500.00    | 1
    2  | test.file2.txt | 100       | S         | 1700.00    | 2

我能夠毫無問題地創建我的批處理集合,而我遇到的問題是弄清楚如何使用其中的正確批處理來創建唯一文件的集合。 我正在嘗試使用以下方法:

    private static RemittanceCenterFilesSummaryListModel SummarizeFiles(RemittanceCenterSummaryListModel remittanceCenterSummaryListModel)
    {
       var summarizedBatches = SummarizeBatches(remittanceCenterSummaryListModel);

                   var fileResult = remittanceCenterSummaryListModel.RemittanceBatchSummaryRecord.GroupBy(x => new { x.FileId, x.SourceFileName })
            .Select(x => new RemitanceCenterFileSummarizedModel()
            {
                FileId = x.Key.FileId,
                SourceFileName = x.Key.SourceFileName,
                ScannedBatchCount = x.Count(y => y.BatchType == "S"),
                ScannedBatchAmount = x.Where(y => y.BatchType == "S").Sum(y => y.PaymentAmount),
                NonScannedBatchCount = x.Count(y => y.BatchType != "S"),
                NonScannedBatchAmount = x.Where(y => y.BatchType != "S").Sum(y => y.PaymentAmount),
            });

        var summaryListModel = CreateSummaryFilesListModel(fileResult);
        summaryListModel.Batches = summarizedBatches.RemittanceBatchSummary; 
        return summaryListModel;
    }
    private static RemittanceCenterFilesSummaryListModel CreateSummaryFilesListModel(IEnumerable<RemitanceCenterFileSummarizedModel> summaryModels)
    {
        var summaryModelList = new RemittanceCenterFilesSummaryListModel();

        foreach (var summaryFileRec in summaryModels)
        {
            var summaryModel = new RemitanceCenterFileSummarizedModel
            {
                FileId = summaryFileRec.FileId.ToString(CultureInfo.InvariantCulture),
                SourceFileName = summaryFileRec.SourceFileName.ToString(CultureInfo.InvariantCulture),
                ScannedBatchCount = summaryFileRec.ScannedBatchCount,
                ScannedBatchAmount = summaryFileRec.ScannedBatchAmount,
                NonScannedBatchCount = summaryFileRec.NonScannedBatchCount,
                NonScannedBatchAmount = summaryFileRec.NonScannedBatchAmount
            };

            summaryModelList.RemittanceFilesSummary.Add(summaryModel);
        }

        return summaryModelList;
    }

您也可以在4列(包括BatchTypeBatchCode ,並選擇Count並求和,如:

 var fileResult = remittanceCenterSummaryListModel.RemittanceBatchSummaryRecord
                     .GroupBy(x => new 
                                  { 
                                    x.FileId,
                                    x.SourceFileName,
                                    x.BatchType,
                                    x.BatchCode 
                                  })
                     .Select(x => new
                                 {
                                   FileId = x.Key.FileId,
                                   SourceFileName = x.Key.SourceFileName,
                                   BatchType = x.Key.BatchType,
                                   BatchCode = x.Key.BatchCode,
                                   BatchTotal= x.Sum(y=>y.PaymentAmt),
                                   RecordCount = x.Count()
                             });

我猜你需要GroupBy FileIdBatchType而不是FileName :-

var fileResult = remittanceCenterSummaryListModel.RemittanceBatchSummaryRecord
            .GroupBy(x => new { x.FileId, x.BatchType })
            .Select(x => 
            { 
                var firstObj = x.FirstOrDefault();
                return new RemitanceCenterFileSummarizedModel()
                { 
                   FileId = x.Key.FileId,
                   SourceFileName = firstObj.SourceFileName,
                   BatchCode = firstObj.BatchCode,
                   BatchType  = x.Key.BatchType,
                   BatchTotal  = x.Sum(z => z.PaymentAmt),
                   RecordCount = x.Count()
                };
            });

考慮到FileId映射到SourceFileName以及BatchCode映射到BatchType您可以像在firstObj那樣簡單地將第一個集合存儲在變量中,以獲取未分組的相關值。 在訪問相關屬性之前,請檢查是否為空,因為如果找不到集合,可能會導致NRE

對於純linq非流利

 var files = new[] { 
                new { FileId = 1, SourceFileName =  "test.file1.txt" , BatchCode = 100 , BatchType = "S", PaymentAmt = 1000.00 , BillingCode = "Exc" },
                new { FileId = 1, SourceFileName =  "test.file1.txt" , BatchCode = 100 , BatchType = "S", PaymentAmt = 2000.00 , BillingCode = "Exc" },
                new { FileId = 1, SourceFileName =  "test.file1.txt" , BatchCode = 200 , BatchType = "N", PaymentAmt = 500.00 , BillingCode = "Adc" },
                new { FileId = 1, SourceFileName =  "test.file2.txt " , BatchCode = 300 , BatchType = "S", PaymentAmt = 1200.00 , BillingCode = "Exc" },
                new { FileId = 1, SourceFileName =  "test.file2.txt " , BatchCode = 300 , BatchType = "S", PaymentAmt = 1500.00 , BillingCode = "Exc" }
            };

            var result = from file in files
                         group file by new { file.FileId, file.BatchCode } into fileBachGroups
                         select new 
                         { 
                             FileId = 1, 
                             SourceFileName = fileBachGroups.First().SourceFileName, 
                             BatchCode = fileBachGroups.Key.BatchCode,
                             BatchType = fileBachGroups.First().BatchType,
                             BatchTotal = fileBachGroups.Sum(f => f.PaymentAmt),
                             RecordCount = fileBachGroups.Count()
                         };

            Console.WriteLine("FileId | SourceFileName | BatchCode | BatchType | BatchTotal |RecordCount");

            foreach (var item in result)
            {
                Console.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}",item.FileId,item.SourceFileName, item.BatchCode, item.BatchType, item.BatchTotal, item.RecordCount);
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM