The first part of my code gets the IQueryable data results:
var issues = repository.GetAllIssues().Where(i =>
i.IssueNotificationRecipients.Any(r => r.Status == "Open"));
Then I determine which sort order the user has requested, and add it:
switch (sort)
{
case 1:
issues.OrderBy(x => x.Customer);
break;
case 2:
issues.OrderBy(x => x.Description);
break;
case 3:
issues.OrderBy(x => x.CreatedBy);
break;
default:
issues.OrderBy(x => x.DueDateTime);
break;
}
This throws the error:
The method '开发者_如何学JAVASkip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'
So how can I add the OrderBy dynamically, in response to my user's input?
Did you perhaps mean
switch (sort)
{
case 1:
issues = issues.OrderBy(x => x.Customer);
break;
case 2:
issues = issues.OrderBy(x => x.Description);
break;
case 3:
issues = issues.OrderBy(x => x.CreatedBy);
break;
default:
issues = issues.OrderBy(x => x.DueDateTime);
break;
}
You may need to change the type of issues
, or set it to a new variable, since it returns IOrderedQueryable<T>
You haven't shown us all of your code, but one problem is that you're expecting issues.OrderBy(..)
to mutate the queryable referred to by the issues
variable. But it doesn't actually do that; it returns a new IOrderedQueryable
that represents an ordered version of the original queryable.
Since your ordering operations aren't actually touching the queryable referred to be your issues
variable; it makes sense that calling Skip
on it results in the error that you're getting, because it hasn't actually been sorted.
You probably want to do:
issues = issues.OrderBy(...);
精彩评论