In Linq-to-Objects, var will be output to IOrderedEnumerable<T> , where T is the type of your object. Take() will give an IEnumerable<T> , so your line of code will not be allowed. (IOrderedEnumerable is more defined than IEnumerable, you need your query to be entered in a less defined way.) And, as the comments note, the same is true for providers who deal with IQueryable<T> terms, which themselves can be expressed as less specified IEnumerable<T> .
To do this, explicitly type your query into the smaller specified type that you need, IEnumerable<T> or IQueryable<T> , and then you can apply your conditional Take .
IEnumerable<YourType> orderedQuery = ... if (condition) orderedQuery = orderedQuery.Take(n);
source share