This line is expensive: var users = query.ToList(); 🧠 Why Executes immediately Loads everything into memory ✅ Better Stream or filter first: var users = query.Where(x => x.IsActive);
Daily micro-tips for C#, SQL, performance, and scalable backend engineering.
This line is expensive: var users = query.ToList(); 🧠 Why Executes immediately Loads everything into memory ✅ Better Stream or filter first: var users = query.Where(x => x.IsActive);
Developers commonly mix async with LINQ: var results = items.Select(async x => await Process(x)); await Task.WhenAll(results); Looks correct, right?Nope. This triggers thousands of async state machines, killing perf. await Parallel.ForEachAsync(items, async (item, ct) => { await Process(item); }); 💡 Why It Saves You uses shared workers avoids micro-task explosion predictable throughput
The formal description for SelectMany() is: Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence. SelectMany() flattens the resulting sequences into one sequence and invokes a result selector function on each element. The main difference is the result of each method while SelectMany() returns flattened results; Select() […]
Whenever you use SingleOrDefault, you clearly state that the query should result in at most a single result. On the other hand, when FirstOrDefault is used, the query can return any amount of results but you state that you only want the first one. If your result set returns 0 records: Single throws an exception First throws an exception SingleOrDefault returns the default […]