Create a fast command-line utility that parses CSV expense or inventory rows into C# record objects, aggregates category totals with LINQ, and exports a clean report.json summary using System.Text.Json.
csharp-csv-auditor/
├── CsvAuditor.csproj # .NET 8 console project
├── Program.cs # LINQ parser + JSON export
└── sample.csv # Test rows to audit
dotnet run -- sample.csv
Check off each milestone as you build. Your progress saves automatically in your browser.
using System.Globalization;
using System.Text.Json;
if (args.Length == 0)
{
Console.Error.WriteLine("Usage: dotnet run -- <path-to-csv>");
return 1;
}
string csvPath = args[0];
if (!File.Exists(csvPath))
{
Console.Error.WriteLine($"Error: File not found -> {csvPath}");
return 1;
}
var rows = new List<ExpenseRow>();
int skippedLines = 0;
foreach (string line in File.ReadLines(csvPath).Skip(1))
{
if (string.IsNullOrWhiteSpace(line)) continue;
string[] parts = line.Split(',', StringSplitOptions.TrimEntries);
if (parts.Length < 3 ||
!decimal.TryParse(parts[2], NumberStyles.Number, CultureInfo.InvariantCulture, out decimal amount))
{
skippedLines++;
continue;
}
rows.Add(new ExpenseRow(parts[0], parts[1], amount));
}
var byCategory = rows
.GroupBy(r => r.Category, StringComparer.OrdinalIgnoreCase)
.Select(g => new CategorySummary(
Category: g.Key,
Count: g.Count(),
Total: g.Sum(x => x.Amount)
))
.OrderByDescending(c => c.Total)
.ToList();
decimal grandTotal = rows.Sum(r => r.Amount);
Console.WriteLine("=== DevBytes .NET CSV Audit Report ===");
Console.WriteLine($"Valid Rows: {rows.Count} (Skipped: {skippedLines})");
Console.WriteLine($"Grand Total: {grandTotal:C2}\n");
foreach (var item in byCategory)
{
Console.WriteLine($" - {item.Category,-14} {item.Count,2} items | {item.Total,10:C2}");
}
var report = new AuditReport(
SourceFile: Path.GetFileName(csvPath),
GeneratedAtUtc: DateTime.UtcNow,
ValidRows: rows.Count,
SkippedRows: skippedLines,
GrandTotal: grandTotal,
Categories: byCategory
);
string jsonPath = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(csvPath))!, "report.json");
string json = JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(jsonPath, json);
Console.WriteLine($"\n[+] JSON report saved -> {jsonPath}");
return 0;
record ExpenseRow(string Item, string Category, decimal Amount);
record CategorySummary(string Category, int Count, decimal Total);
record AuditReport(
string SourceFile,
DateTime GeneratedAtUtc,
int ValidRows,
int SkippedRows,
decimal GrandTotal,
List<CategorySummary> Categories
);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Item,Category,Amount
Domain Renewal,Infrastructure,12.99
Cloud VPS,Infrastructure,24.00
Mechanical Keycaps,Hardware,48.50
USB-C NVMe Enclosure,Hardware,29.95
API Load Test Credits,Testing,15.00