LinqToCsv LINQtoCSV void Main() { var path = @"D:\Users\tlamb\Downloads"; var utilizationFile = @"CI_Sepsis LOS - Pharmacy_All Physicians_Utilization_01-15-2021.csv"; //@"CI_Sepsis LOS - Pharmacy_All Physicians_Utilization_01-14-2021.csv"; var trackingDetailsFile = @"CI_Sepsis LOS - Pharmacy_01-14-2021 Tracking Details.csv"; var inputFileDescription = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true }; var cc = new CsvContext(); var trackingDetails = cc.Read(Path.Combine(path, trackingDetailsFile), inputFileDescription) .GroupBy(td => td.Charge_Description, (g, d) => new Consolidated { Charge_Code = g, TotalDirectVariableCost = d.Sum(u => u.TotalDirectVariableCost) }) .OrderBy(td => td.Charge_Code); var utilization = cc.Read(Path.Combine(path, utilizationFile), inputFileDescription) .GroupBy(u => u.Charge_Code, (g, d) => d.Select(u => new Consolidated { Charge_Code = u.Charge_Code, Baseline_TotalDirectVariableCost = u.Baseline_TotalDirectVariableCost, Tracking_TotalDirectVariableCost = u.Tracking_TotalDirectVariableCost })) .SelectMany(u => u) .OrderBy(u => u.Charge_Code); var consolidated = from td in utilization join u in trackingDetails on td.Charge_Code equals u.Charge_Code into g from x in g.DefaultIfEmpty() select new Consolidated { Charge_Code = x?.Charge_Code ?? td.Charge_Code, TotalDirectVariableCost = x?.TotalDirectVariableCost ?? 0.0d, Baseline_TotalDirectVariableCost = td?.Baseline_TotalDirectVariableCost ?? 0.0d, Tracking_TotalDirectVariableCost = td?.Tracking_TotalDirectVariableCost ?? 0.0d }; cc.Write(consolidated, Path.Combine(path, "consolidated.csv"), inputFileDescription); } // Define other methods and classes here public class TrackingDetails { public string Encounter { get; set; } public DateTime Admit_Date { get; set; } public DateTime Discharge_Date { get; set; } public string Physician { get; set; } public string Physician_Group { get; set; } public DateTime Date_of_Service { get; set; } public string Rollup { get; set; } public string Charge_Description { get; set; } public int UOS { get; set; } public string Unit_Direct_Variable_Cost { get; set; } public string Total_Direct_Variable_Cost { get; set; } public double TotalDirectVariableCost { get { return double.TryParse(Total_Direct_Variable_Cost.Replace("$", "").Replace("(", "-").Replace(")", ""), out var result) ? result : 0.0d; } } } public class Utilization { public string Rollup { get; set; } public string Charge_Code { get; set; } public string Direct_Variable_Cost { get; set; } public string x { get; set; } public string Cases1 { get; set; } public string Quantity1 { get; set; } public string Pct_of_Total_Cases1 { get; set; } public string Qty_Used_Cases1 { get; set; } public string Cases2 { get; set; } public string Quantity2 { get; set; } public string Pct_of_Total_Cases2 { get; set; } public string Qty_Used_Cases2 { get; set; } public string Pct_of_Total_Cases3 { get; set; } public string Qty_Used_Cases3 { get; set; } public string b { get; set; } public string CostPerCase1 { get; set; } public string CostPerCase2 { get; set; } public string CostPerCase3 { get; set; } public string Total_Direct_Variable_Cost1 { get; set; } public string Total_Direct_Variable_Cost2 { get; set; } public string Total_Direct_Variable_Cost3 { get; set; } public double Baseline_TotalDirectVariableCost { get { return string.IsNullOrEmpty(Total_Direct_Variable_Cost1) ? 0.0d : double.TryParse(Total_Direct_Variable_Cost1.Replace("$-", "0").Replace("$", "").Replace("(", "-").Replace(")", ""), out var result) ? result : 0.0d; } } public double Tracking_TotalDirectVariableCost { get { return string.IsNullOrEmpty(Total_Direct_Variable_Cost2) ? 0.0d : double.TryParse(Total_Direct_Variable_Cost2.Replace("$-", "0").Replace("$", "").Replace("(", "-").Replace(")", ""), out var result) ? result : 0.0d; } } } public class Consolidated { public string Charge_Code { get; set; } public double TotalDirectVariableCost { get; set; } public double Baseline_TotalDirectVariableCost { get; set; } public double Tracking_TotalDirectVariableCost { get; set; } }