LINQ GroupBy in C#: Group, Count and Sum a List

LINQ GroupBy in C#: sales grouped by state with a count and a dollar total per group, TX 3 sales $3,400, CA $2,650, NY $2,000

For a C# LINQ group by, call GroupBy with a key selector such as s => s.State. You get one group per key, and each group carries its Key plus its items, so Count() and Sum() work per group: Below, the same eight US sales get grouped by two columns, filtered like SQL HAVING, turned … Read more >>

Nullable Reference Types in C#: Enable Them and Fix the Warnings

C# nullable reference types: a public string Name field raising warning CS8618, fixed by declaring it required string Name

The most common mistake when a team turns on C# nullable reference types is silencing every warning with the ! operator. The build goes quiet, but the app still crashes, because ! only hides the warning: Quick answer: keep <Nullable>enable</Nullable> on, mark values that can be missing as string?, and fix each warning with a … Read more >>

System.Text.Json in C#: Serialize and Deserialize JSON (.NET 10 Examples)

System.Text.Json in C#: JsonSerializer.Serialize turns new Order(1001, 249.99m) into the JSON string from the real .NET 10 run

To turn a C# object into JSON with System.Text.Json, call JsonSerializer.Serialize; to read it back, call JsonSerializer.Deserialize<T>. Both live in the System.Text.Json namespace that ships with .NET: I ran every example below on .NET 10 against one US customer order, including the native AOT default that breaks reflection JSON in file-based apps. The Sample Order … Read more >>