在一个列表中具有多个条件的Linq Multiple Orderby查询

时间:2019-05-29 09:22:09

标签: asp.net-mvc linq asp.net-core linq-to-sql linq-to-entities

var result = await projectRepo.GetPagedListAsync(x => new ProjectApiModel
            {
                ProjectId = x.Id,
                ProjectKey = x.ProjectKey,
                ProjectName = x.ProjectName,
                ProjectStatus = x.ProjectStatus,
                Tasks = x.Tasks.Where(z => /*z.AgentId == referenceId &&*/ z.Status == true && (z.TaskStatus == null || z.TaskStatus.StatusName != "Closed")).Select(y => new TaskApiModel
                {
                    TaskId = y.Id,
                    TaskName = y.TaskName,
                    ProjectId = y.ProjectId,
                    TaskTypeName = y.TaskType.TypeName,
                    TaskPriority = y.TaskPriorityId == null ? null : new PriorityModel { Name = y.TaskPriority.Name, Color = y.TaskPriority.Color, Id = y.TaskPriority.Id, Scale = y.TaskPriority.Scale },
                    TaskStatus = y.TaskStatusId == null ? null : new StatusModel { StatusName = y.TaskStatus.StatusName, Color = y.TaskStatus.Color, Scale = y.TaskStatus.Scale, StatusId = y.TaskStatus.Id, Status = y.TaskStatus.Status },
                    Deadline=y.EndDateUTC
                }).ToList()
            },orderBy: orders => orders.OrderBy(o => o.ProjectName));

这就是我用来对项目列表和项目名称进行排序的方法。现在,我想按条件更改订单,如下所示。

  1. 首先要显示所有包含TaskPriority的项目为“紧急” 在任何一项任务中(每个项目都有任务列表)。
  2. 其余所有项目都想显示升序。
  3. 如果任何以数字开头的项目名称都应放在最后。

我尝试了代码

orderBy: orders => orders.OrderBy(o => o.Tasks.Any(s=>s.TaskPriority.Name.ToLower()== "urgent")).ThenBy(i=>i.ProjectName));

1 个答案:

答案 0 :(得分:1)

希望我能正确理解您的问题。您的查询无法正常工作的原因是在对布尔值进行排序时,首先对False进行了排序。

您需要做的是在第一个条件下按降序排序。

orders.OrderByDescending(o => o.Tasks.Any(s=>s.TaskPriority.Name.ToLower()== "urgent"))
       .ThenBy(i=>i.ProjectName));

例如,使用更简单的类模拟您的方案

public class Project
{
    public long ProjectId{get;set;}
    public string ProjectName{get;set;}
    public IEnumerable<ProjectTask> Task{get;set;}
}

public class ProjectTask
{
    public long TaskId{get;set;}
    public string TaskPriority{get;set;}
}

客户代码

var project1 = new Project
{
    ProjectId=1,
    ProjectName = "abc",
    Task = new []
    {
        new ProjectTask{TaskId=1,TaskPriority="urgent"},
        new ProjectTask{TaskId=1,TaskPriority="moderate"},
    }
};

var project2 = new Project
{
    ProjectId=2,
    ProjectName = "aaa",
    Task = new []
    {
        new ProjectTask{TaskId=1,TaskPriority="moderate"},
        new ProjectTask{TaskId=1,TaskPriority="moderate"},
    }
};


var project3 = new Project
{
    ProjectId=2,
    ProjectName = "abb",
    Task = Enumerable.Empty<ProjectTask>()
};

var project4 = new Project
{
    ProjectId=2,
    ProjectName = "abaa",
    Task = new []
    {
        new ProjectTask{TaskId=1,TaskPriority="moderate"},
        new ProjectTask{TaskId=1,TaskPriority="moderate"},
    }
};

var listOfProjects = new [] {project1,project2,project3,project4};

var r = listOfProjects.OrderByDescending(o => o.Task.Any(s=>s.TaskPriority.ToLower()== "urgent"))
                      .ThenBy(i=>i.ProjectName);

样本输出

enter image description here