使用Linq对列表及其所有嵌套对象进行排序
有一个组织有几个部门,每个部门都有一些员工。 我的对象结构如下所示:
public class Organisation { public int Code { get; set; } public string Type { get; set; } public string Name { get; set; } public List Departments { get; set; } } public class Department { public int Code { get; set; } public string Name { get; set; } public List Employees { get; set; } } public class Employee { public int Code { get; set; } public string Name { get; set; } }
如果我有这些组织的列表,使用linq,我想按照代码和名称对列表中的所有组织进行排序,然后按代码和名称对所有部门进行排序,然后按代码和名称对所有员工进行排序。 以下是我填充的一些测试数据:
var britishTelecomLtd = new Organisation { Code = 8, Name = "British Telecom Ltd", Type = "Institutional", Departments = new List { new Department { Code = 6, Name = "Finance", Employees = new List { new Employee { Code = 5, Name = "Peter" }, new Employee { Code = 2, Name = "James" }, new Employee { Code = 6, Name = "Andrew" } } }, new Department { Code = 5, Name = "Accounts", Employees = new List { new Employee { Code = 15, Name = "Jane" }, new Employee { Code = 22, Name = "John" }, new Employee { Code = 16, Name = "Mark" } } } } }; var virginMediaLtd = new Organisation { Code = 5, Name = "Virgin Media Ltd", Type = "Institutional", Departments = new List { new Department { Code = 6, Name = "Sales", Employees = new List { new Employee { Code = 5, Name = "Peter" }, new Employee { Code = 2, Name = "James" }, new Employee { Code = 6, Name = "Andrew" } } }, new Department { Code = 5, Name = "Support", Employees = new List { new Employee { Code = 15, Name = "Jane" }, new Employee { Code = 22, Name = "John" }, new Employee { Code = 16, Name = "Mark" } } } } }; var pcWorldLtd = new Organisation { Code = 18, Name = "PC World Ltd", Type = "Retail", Departments = new List { new Department { Code = 6, Name = "Marketing", Employees = new List { new Employee { Code = 15, Name = "Jane" }, new Employee { Code = 22, Name = "John" }, new Employee { Code = 16, Name = "Mark" } } }, new Department { Code = 5, Name = "Customer Services", Employees = new List { new Employee { Code = 5, Name = "Kelly" }, new Employee { Code = 2, Name = "Jenny" }, new Employee { Code = 6, Name = "Tricia" } } } } }; var blueCatLtd = new Organisation { Code = 3, Name = "Blue Cat Music Ltd", Type = "Retail", Departments = new List { new Department { Code = 6, Name = "Sales", Employees = new List { new Employee { Code = 5, Name = "Peter" }, new Employee { Code = 2, Name = "James" }, new Employee { Code = 6, Name = "Andrew" } } }, new Department { Code = 5, Name = "Warehouse", Employees = new List { new Employee { Code = 5, Name = "Andy" }, new Employee { Code = 2, Name = "Robert" }, new Employee { Code = 6, Name = "Dave" } } } } }; var organisations = new List { britishTelecomLtd, virginMediaLtd, pcWorldLtd, blueCatLtd };
在这里,我将数据添加到字典中
var legalEntitiesCollectionByType = new Dictionary<string, ICollection> { { "Institutional", organisations .Where(x => x.Type == "Institutional") .OrderBy(x => x.Code).ThenBy(x => x.Name) .ToList() }, { "Retail", organisations .Where(x => x.Type == "Retail") .OrderBy(x => x.Code).ThenBy(x => x.Name) .ToList() } };
但正如您所看到的那样,排序只发生在组织层面而不是部门或员工层面? 我的问题是如何在填充上面的字典时在对象层次结构中实现所有3个级别的排序?
干杯
您需要在返回的对象中进行所有三个级别的排序,如下所示(我只显示"Retail"
, "Institutional"
需要以相同的方式排序):
{ "Retail", organisations .Where(x => x.Type == "Retail") .OrderBy(x => x.Code).ThenBy(x => x.Name) .Select(x => new Organisation { x.Code , x.Type , x.Name , Departments = x.Departmentsd.OrderBy(d => d.Code).ThenBy(d => d.Name) .Select(d => new Department { d.Code , d.Name , Employees = d.Employees.OrderBy(e => e.Code).ThenBy(e => e.Name).ToList() }) }).ToList() }
由于您需要多次选择此选项,因此您可能希望将此代码包装在方法中,并从多个位置使用它,如下所示:
private Organisation SortedOrganisation(Organisation x) { return new Organisation { x.Code , x.Type , x.Name , Departments = x.Departmentsd.OrderBy(d => d.Code).ThenBy(d => d.Name) .Select(d => new Department { d.Code , d.Name , Employees = d.Employees.OrderBy(e => e.Code).ThenBy(e => e.Name).ToList() }) }; } ... var legalEntitiesCollectionByType = new Dictionary> { { "Institutional", organisations .Where(x => x.Type == "Institutional") .OrderBy(x => x.Code).ThenBy(x => x.Name) .Select(SortedOrganisation) .ToList() }, { "Retail", organisations .Where(x => x.Type == "Retail") .OrderBy(x => x.Code).ThenBy(x => x.Name) .Select(SortedOrganisation) .ToList() } };
我知道这是一个老问题,但是有一种更简单的方法可以实现相同的结果:
organisations = organisations.OrderBy(org => { org.Departments = org.Departments .OrderBy(dept => { dept.Employees = dept.Employees .OrderBy(employee => employee.Code) .ThenBy(employee=>employee.Name); return dept.Code; }) .ThenBy(dept=>dept.Name); return org.Code; }) .ThenBy(org=>org.Name);
如果需要按代码和名称对Employees
进行排序,则可以将该属性设置为SortedList<>
。
public class Department { ... public SortedList, Employee> Employees { get; set; } }
在.NET 4之前,您可以使用KeyValuePair
而不是Tuple
。
创建Employees
对象时,您需要为排序列表的键提供IComparer
对象。
Employees = new SortedList, Employee>(new EmployeeKeyComparer());
其中EmployeeKeyComparer
可以定义为
public class EmployeeKeyComparer : IComparer> { public int Compare(Tuple x, Tuple y) { if (x.First == y.First) return StringComparer.Ordinal.Compare(x.Second, y.Second); else return x.First.CompareTo(y.First); } }
您可以在之前排序:
organisations.ToList().ForEach(o => o.Departments = o.Departments.OrderBy(d => d.Code).ToList()); organisations.SelectMany(o => o.Departments).ToList().ForEach(d => d.Employees = d.Employees.OrderBy(e => e.Name).ToList());
然后使用已排序的列表
var legalEntitiesCollectionByType = new Dictionary> { { "Institutional", organisations .Where(x => x.Type == "Institutional") .ToList() }, { "Retail", organisations .Where(x => x.Type == "Retail") .ToList() } };
注意:排序不到位,您可以使用comparer
实现此目的
上述就是C#学习教程:使用Linq对列表及其所有嵌套对象进行排序分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
organisations.ToList().ForEach(o => o.Departments.Sort(CreateCustomComparison)); organisations.SelectMany(o => o.Departments).ToList().ForEach(d => d.Employees.Sort(CreateCustomComparison));
var legalEntitiesCollectionByType = new Dictionary> { { "Institutional", organisations .Where(x => x.Type == "Institutional") .ToList() .Select(o => new Organisation{Code = x.Code,Departaments = x.Departaments.OrderBy(c => c).ToList() }).ToList() } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/998767.html