The first version will not even compile, because the return value of Take is IEnumerable<T> , not List<T> . Therefore, you will need:
public List<Log> GetLatestLogEntries() { var logEntries = from entry in db.Logs select entry; return logEntries.ToList().Take(10).ToList(); }
This will allow you to get all the data from the database and convert it to a list, and then take the first 10 records, and then convert them to a list again.
Getting Take(10) in the database (i.e. the second form) certainly looks a lot cheaper for me ...
Note that there is no Queryable.ToList() method - you end up calling Enumerable.ToList() , which will retrieve all the records. In other words, the ToList call ToList not involved in the SQL translation, whereas Take does.
Also note that using a query expression here doesn't make much sense either. I would write this as:
public List<Log> GetLatestLogEntries() { return db.Log.Take(10).ToList(); }
Keep in mind, you may need to call OrderBy - otherwise it will just take the first 10 records it finds, which may not be the very last ...
Jon skeet
source share