订购GroupBy Asc基于使用LINQ C#的另外两个属性

时间:2016-03-22 12:21:24

标签: c# linq sorting group-by

我的GroupBy表现不错。我得到了输出

enter image description here

  

我需要对组名进行排序

     

Brown Color Block代表集团。

     

布朗颜色块中的红色框表示管理器

     
      
  1. Peter Block(布朗盒子)应该先来
  2.   
  3. Raj Block(布朗盒子)应该来第二个
  4.   
  5. Sunny Block(布朗盒子)应该来第三个
  6.         

    每个区块应由Boss(经理)和助理(Boss没有   SID)。在GroupBy之后,名称应该在组内的排序顺序中   助理名称也在排序顺序中。

模型类:

public class Person
{
    public int ID { get; set; }
    public int SID { get; set; }
    public string Name { get; set; }
    public string Department { get; set; }
    public string Gender { get; set; }
    public string Role { get; set; }
}

public class Boss
{
    public int ID { get; set; }
    public int SID { get; set; }
    public string Name { get; set; }
    public string Department { get; set; }
    public string Gender { get; set; }
    public string Role { get; set; }
    public List<Person> Employees { get; set; }
}

主要功能源代码:

void Main()
{
    List<Boss> BossList = new List<Boss>()
    {
        new Boss()
        {
            ID = 101,
            Name = "Sunny",
            Department = "Development",
            Gender = "Male",
            Role = "Manager",
            Employees = new List<Person>()
            {
                new Person() {ID = 101, SID = 102, Name = "Peter", Department = "Development", Gender = "Male", Role = "Assistant"},
                new Person() {ID = 101, SID = 103, Name = "Emma Watson", Department = "Development", Gender = "Female", Role = "Assistant"},

            }
        },
        new Boss()
        {
            ID = 104,
            Name = "Raj",
            Department = "Development",
            Gender = "Male",
            Role = "Manager",
            Employees = new List<Person>()
                    {
                        new Person() {ID = 104, SID = 105, Name = "Kaliya", Department = "Development", Gender = "Male", Role = "Assistant"},
                        new Person() {ID = 104, SID = 103, Name = "Emma Watson", Department = "Development", Gender = "Female", Role = "Assistant"},

                    },
        },
        new Boss()
        {
            ID = 102,
            Name = "Peter",
            Department = "Development",
            Gender = "Male",
            Role = "Manager",
            Employees = new List<Person>()
                    {
                        new Person() {ID = 102, SID = 105, Name = "Kaliya", Department = "Development", Gender = "Male", Role = "Assistant"},
                        new Person() {ID = 102, SID = 103, Name = "Emma Watson", Department = "Development", Gender = "Female", Role = "Assistant"},

                    }
        }
    };

    List<Person> EmpList = BossList.SelectMany(i =>
        new[] {
                new Person()
                {
                    ID = i.ID,
                    SID = i.SID,
                    Name = i.Name,
                    Gender = i.Gender,
                    Department = i.Department,
                    Role = i.Role
                }
        }.Concat(i.Employees)
    ).ToList().GroupBy(s => s.ID).SelectMany(h => h.GroupBy(g => g.SID).SelectMany(u => u.OrderBy(k=> k.Name))).ToList();
}

2 个答案:

答案 0 :(得分:-1)

您可以通过在Order by之后添加ThenBy扩展方法来应用辅助排序。事实上,可以多次调用ThenBy来对多个属性进行排序。我修改了代码的最后一行,以展示如何实现这一目标。

).ToList().GroupBy(s => s.ID).SelectMany(h => h.GroupBy(g => g.SID).SelectMany(u => u.OrderBy(k=> k.Name).ThenBy(l => l.<<secondproperty>>))).ToList();

答案 1 :(得分:-1)

数据结构已经建立了组。没有必要重新分组。

List<Person> result = (
  from boss in BossList
  order by boss.Name
  let orderedEmployees = boss.Employees.OrderBy(emp => emp.Name)
  let bossPerson = new Person(boss)
  let people = new List<Person>() { bossPerson }.Concat(orderedEmployees)
  from person in people
  select person).ToList();

如果您更喜欢lambda语法:

List<Person> result = BossList
  .OrderBy(boss => boss.Name)
  .SelectMany(boss => {
    IEnumerable<Person> orderedEmployees = boss.Employees.OrderBy(emp => emp.Name);
    Person bossPerson = new Person(boss);
    return new List<Person>() { bossPerson }.Concat(orderedEmployees);
  })
  .ToList();