1. Use AsAsyncEnumerable()
for Streaming
EF Core 3.0+ supports AsAsyncEnumerable()
, which streams results incrementally (similar to a cursor):
using Microsoft.EntityFrameworkCore; using System.Linq; var dbContext = new YourDbContext(); // Replace with your DbContext var query = dbContext.YourEntities.AsNoTracking().AsAsyncEnumerable(); await foreach (var entity in query) { // Process one entity at a time DateTime utcTime = entity.Time.ToUniversalTime(); long epochSeconds = (long)(utcTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; // ... }
2. Use Raw SQL with Streaming
For complex queries, execute raw SQL and stream results:
var sql = "SELECT * FROM your_table"; var query = dbContext.YourEntities .FromSqlRaw(sql) .AsAsyncEnumerable(); await foreach (var entity in query) { // Process entity }