我的代码的第一部分获取IQueryable数据结果:

var issues = repository.GetAllIssues().Where(i =>
                  i.IssueNotificationRecipients.Any(r => r.Status == "Open"));


然后确定用户请求的排序顺序,并添加它:

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;
}


这引发了错误:


  仅支持“跳过”方法
  用于LINQ to Entities中的排序输入。
  方法“ OrderBy”必须调用
  在“跳过”方法之前


那么,如何响应用户的输入动态添加OrderBy?

最佳答案

你可能是说

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;
}


您可能需要更改issues的类型,或将其设置为新变量,因为它返回IOrderedQueryable<T>

08-06 18:51