LINQ (when talking to the database) is usually an unbuffered buffering API. To do what you want, follow these steps:
- enable multiple active result sets (MARS)
- data buffer first
I prefer the second option; it just includes adding .ToList() to your first line:
var elements = conn.MyTable.Where(x => x.id == id && x.name == name) .OrderBy(x => x.id).OrderBy(x => x.name). .Select(t => new { t.id, t.name, }).GroupBy(t => new { t.id, t.name, }) .ToList();
now that this line has started, we know that we already have all the data in memory, and the reader has closed; previously, he could still talk about entering a line from the reader.
For completeness, the inclusion of MARS is discussed here - this would not be my recommendation.
Marc gravell
source share