I don't think you can put a condition in a larger query, but what you could do is split it into another C # statement, for example:
// Common code: var hosters = from e in context.Hosters_HostingProviderDetail where e.ActiveStatusID == pendingStateId; // The difference between ASC and DESC: hosters = (sortOrder == SortOrder.ASC ? hosters.OrderBy(e => e.HostingProviderName) : hosters.OrderByDescending(e => e.HostingProviderName)); // More common code: returnList = hosters.ToList<Hosters_HostingProviderDetail>();
source share