SQL Server 2008 and Entity Framework 4
The table I have
There is a column in each row.
The user may choose a period of time in the past, and I must get the
closest to the time, however that had already passed when the moment came. In other words, the final order placed before to the deadline.
As an example, if I have orders
2008-01-12 2009-04-17 2009-09-24 2010-11-02 2010-12-01 2011-05-16
and choose a date
I should purchase the
since it is the final order placed before the deadline.
var query = (from oData in db.OrderDatas where oData.Timestamp <= userTime orderby oData.Timestamp ascending select oData).Last();
The closest to what I'm attempting is this. But if the Last operator is translated at all, I'm not sure how it works precisely in SQL.
Does this query return all data (before to
) and extract the last element, or will it be translated such that the database returns only one element? Performance is a problem since my table has a very huge capacity (100000+ rows).
Additionally, how might the database's closest time (not necessarily the earlier time) be retrieved? As in the case of
one would acquire
due to the fact that it is closer to the designated date than
In general, you should look at what happens with the SQL if you're worried about how LINQ behaves. The very next thing you should do is figure out how to see how your LINQ queries are converted into SQL, if you haven't already.
is not a feature of LINQ to SQL., as you said in your response, may apply to EF as well. Thankfully, it's simple to use.
var query = (from oData in db.OrderDatas where oData.Timestamp <= userTime orderby oData.Timestamp descending select oData).First();
var query = (from oData in db.OrderDatas where oData.Timestamp <= userTime orderby oData.Timestamp descending select oData).Take(1);
It's on par with TOP 1.