Urgent.News

What's breaking now, across thousands of outlets.

Tech

Async LINQ: Why ToListAsync Exists (And Why WhereAsync Doesn't)

Async LINQ: Why ToListAsync Exists (And Why WhereAsync Doesn't) You write await dbContext.Products.ToListAsync() . Good. Then you wonder: where's WhereAsync ? Why isn't await dbContext.Products.WhereAsync(p => p.Price > 100) ? The answer reveals something fundamental about how LINQ actually works. Where the Async Boundary Lives LINQ operators like Where , Select , OrderBy don't execute anything.…

In LINQ, operators like Where, Select, and OrderBy do not execute any operations. They create a query - an expression tree - that gets executed at a terminal operator such as ToList, First, Count, or Any. This is when data is transferred over the network. It's at this point that one would typically use an async method, hence the existence of ToListAsync. However, there is no WhereAsync because the Where operator does not interact with the database. If it had an async version, it would not add any value.

Entity Framework Core provides async versions of all operators that trigger execution, including ToListAsync, ToArrayAsync, ToDictionaryAsync, CountAsync, SumAsync, AverageAsync, MaxAsync, FirstAsync, FirstOrDefaultAsync, SingleAsync, SingleOrDefaultAsync, AnyAsync, AnyAsync, AllAsync, FindAsync, and others. Each one triggers actual database communication and should be awaited.

Additionally, there's a streaming async pattern - ToListAsync waits for all rows before returning, which could be inefficient for large result sets. The AsAsyncEnumerable() method can be used instead, which asynchronously fetches items one at a time, preventing the entire result set from being held in memory.

In async code, ConfigureAwait(false) is often used to avoid capturing the synchronization context, preventing deadlocks and improving performance in application code. But for library code, this should always be added. Mixing sync and async calls can also be problematic. For example, awaiting OnCompleted() in an async method blocks the thread. Similarly, using Task.Run() with async operations is not ideal as it does not release the thread during I/O operations.

Another common pitfall is the false async, where Task.Run is used to execute a query and await it. This does not make the query async; it merely runs it on a different thread. Parallel execution of DbContext operations is also a mistake because DbContext is not thread-safe. Instead, separate DbContext instances should be used for parallel operations.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

iraca: verificación diferencial independiente de ML-KEM (FIPS 203) en RustCrypto

Qué hicimos iraca es un arnés que verifica la criptografía post-cuántica que el ecosistema Rust realmente usa, diferenciándola contra una implementación independiente .

  • IRACA developed harness to verify ML-KEM (FIPS 203) in RustCrypto
  • RustCrypto crate ml-kem identical to pq-crystals reference in key generation
  • IRACA's method ensures functional differential testing with 128 seeds

Adobe Commerce Added an MCP Layer: Your Catalog Is Now an Agent's Tool

On Thursday, April 9, 2026, an AI shopping agent could call search_shop_catalog on any Shopify store's Storefront MCP endpoint and get products back.

  • Adobe Commerce introduced Commerce MCP server on April 9, 2026.
  • Catalog data now accessible to AI agents in real time.
  • Tool renamed to searchcatalog within nested catalog object.

More from Monday 28 September →