# Improve speed of LINQ query

How can I improve the speed of this LINQ query?

using (var txn = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }))
{

}


It seems like the order by date is the longest process in this query. Is this correct? The database has about a million records. NumberOfRows is max 200. Time of this query is 45ms (after using only one orderby). Is there something more that I can do to improve that LINQ query?

• you have 3 OrderByDescendings followed by takes of which you can remove all but the last – ratchet freak Nov 22 '13 at 14:02
• @ratchetfreak, explain that more in an answer, that is a review of the code, and is a good answer as long as you explain it a little bit more. – Malachi Nov 22 '13 at 15:01

First lets split up the query so we can read it:

var alertList = from alerts in _db.Alerts.AsNoTracking()


the last operation of code has 3 OrderByDescending(a => a.AlertID).take(Id.NumberOfRows) while only 1 is needed
var myAlertsList = alertList.Concat(subAlertList).OrderByDescending(a => a.AlertID).Take(Id.NumberOfRows);