Project · Data

Invoice report

Group paid invoices by customer and render deterministic decimal totals.

All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.

Program.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

var invoices = new[] { new Invoice("Ada", 12.50m, true), new Invoice("Lin", 8m, false), new Invoice("Ada", 4.25m, true), new Invoice("Sam", 9m, true) };
foreach (var row in InvoiceReport.Build(invoices)) Console.WriteLine(row);

Models/Invoice.cs

public sealed record Invoice(string Customer, decimal Amount, bool Paid);

Reports/InvoiceReport.cs

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
public static class InvoiceReport
{
    public static string[] Build(IEnumerable<Invoice> invoices) => invoices.Where(x => x.Paid).GroupBy(x => x.Customer).OrderBy(x => x.Key, System.StringComparer.Ordinal).Select(g => g.Key + ": " + g.Sum(x => x.Amount).ToString("0.00", CultureInfo.InvariantCulture)).ToArray();
}

Invoicereport.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>