Created
September 9, 2022 07:57
-
-
Save nramsbottom/ac11f6293a430c83c07ba6902a02ec15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
using System.Text; | |
using CsvHelper; | |
using CsvHelper.Configuration; | |
var sourcePath = @"C:\Users\nramsbottom\Desktop\Food Shopping.csv"; | |
var reader = new StreamReader(sourcePath, Encoding.UTF8); | |
var parser = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) | |
{ | |
HasHeaderRecord = true | |
}); | |
parser.Context.RegisterClassMap<ReceiptLineMap>(); | |
var receiptLines = new List<ReceiptLine>(); | |
while (await parser.ReadAsync()) | |
{ | |
receiptLines.Add(parser.GetRecord<ReceiptLine>()); | |
} | |
var groupedByStore = receiptLines.GroupBy(x => x.StoreName); | |
var groupedByReceiptNumber = groupedByStore.GroupBy(x => x.GroupBy(y => y.ReceiptNumber)).ToArray(); | |
foreach(var y in groupedByReceiptNumber) | |
{ | |
Console.WriteLine($"{y.Key}"); | |
} | |
var x = 1; | |
class ReceiptLine | |
{ | |
public string ReceiptNumber { get; set; } | |
public DateTime Date { get; set; } | |
public string StoreName { get; set; } | |
public string ProductName { get; set; } | |
public decimal Price { get; set; } | |
public string Currency { get; set; } | |
public decimal? Quantity { get; set; } | |
public decimal? UnitPrice { get; set; } | |
public string? UnitSize { get; set; } | |
public int? TaxPercent { get; set; } | |
} | |
class ReceiptLineMap : ClassMap<ReceiptLine> | |
{ | |
public ReceiptLineMap() | |
{ | |
Map(x => x.ReceiptNumber) | |
.Index(0); | |
Map(x => x.Date) | |
.Index(1); | |
Map(x => x.StoreName) | |
.Index(2); | |
Map(x => x.ProductName) | |
.Index(3); | |
Map(x => x.Price) | |
.Index(4); | |
Map(x => x.Currency) | |
.Index(5); | |
Map(x => x.Quantity) | |
.Index(6); | |
Map(x => x.UnitPrice) | |
.Index(7); | |
Map(x => x.UnitSize) | |
.Index(8); | |
Map(x => x.TaxPercent) | |
.Index(9); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment