First off:
specifically at the bottom where it converts the IQuery object to a List
Yes, that's where you'd expect the Out Of Memory condition to occur.
The assignment of cht
above doesn't actually hit the database; all it does is declare the shape of the query. This is called deferred execution and LINQ uses it all over the place. It means "we don't actually process anything until your code needs it."
Calling ToList
, though, essentially says "the code needs it, all of it, right now." So that's where it sends the query to the database, pulls back all the results all at once, uses LINQ magic to turn them into CLR objects, and stuff them all in a List<T>
for you.
Having said that, this is just a hunch, but it's possible that your LINQ provider doesn't know what Convert.ToDateTime
is. If it doesn't know how to handle that, it won't put it into the WHERE clause in the query it executes, and instead it will load the entire table and filter it client-side, which might be why you crash when the table gets too big, rather than when the result set gets too big.
To verify this, use a profiler for your database to intercept the query, and see if the WHERE clause looks like you'd expect. If it's not translating right, try this instead:
var startTime = Convert.ToDateTime(start);
var endTime = Convert.ToDateTime(end);
var cht = from p in _db.TickerData
where p.Time >= startTime && p.Time <= endTime
orderby p.Time
select p;
_prices = cht.ToList();
If that doesn't help, you're probably just pulling back too much data, and you'll have to work on that the same ways you'd work with processing too much data in any other context.
_prices
? After that we can tell you how to fix the problem. – Intumescence