我有以下列表。
List<Employee> employees = new List<Employee>()
{
new Employee { EmpID = 1 , Name ="AC", DateofPresentation=newDateTime(2013,12,24),new List<Employee>{}
new Employee { EmpID = 1 , Name ="AC", DateofPresentation=newDateTime(2013,12,23), new List<Employee>{}
new Employee { EmpID = 1 , Name ="AC", DateofPresentation=newDateTime(2013,12,22),new List<Employee>{}
}
我需要像这样排序。我有一张表格,显示特定的员工数据。我需要将项目显示为像UI这样的父项。所以我需要这个子列表。
List<Employee> employees = new List<Employee>()
{
new Employee
{
EmpID = 1 , Name ="AC", DateofPresentation=newDateTime(2013,12,24),
new list<Employee>
{
new Employee { EmpID = 1 , Name ="AC" DateofPresentation=newDateTime(2013,12,23), new List<Employee>{}
new Employee { EmpID = 1 , Name ="AC", DateofPresentation=newDateTime(2013,12,22), new List<Employee>{}
}
}
我需要将最近过时的员工作为父母进行排序,并将其他员工排在列表中
我可以在linq中实现这个目标吗?
答案 0 :(得分:0)
以下是代码解决方案:
List<Employee> employees = new List<Employee>()
{
new Employee() { EmpID = 1 , Name ="AC", DateofPresentation=new DateTime(2013,12,24)},
new Employee() { EmpID = 1 , Name ="AC", DateofPresentation=new DateTime(2013,12,23)},
new Employee() { EmpID = 1 , Name ="AC", DateofPresentation=new DateTime(2013,12,22)},
new Employee() { EmpID = 1 , Name ="AC", DateofPresentation=new DateTime(2013,12,21)},
new Employee() { EmpID = 2 , Name ="AC", DateofPresentation=new DateTime(2013,12,25)},
new Employee() { EmpID = 2 , Name ="AC", DateofPresentation=new DateTime(2013,12,21)}
};
var list = employees.
Select(i => new Employee()
{
EmpID = i.EmpID,
Name = i.Name,
DateofPresentation = i.DateofPresentation,
Employees = employees.Where(j => i.EmpID == j.EmpID && j.DateofPresentation < i.DateofPresentation).ToList()
}).
GroupBy(x => x.EmpID).
Select(g => g.OrderByDescending(i => i.DateofPresentation).First()).
ToList();
答案 1 :(得分:0)
您需要将一个属性(我称之为Others
)添加到Employee
以存储列表
employees
.GroupBy(x => x.EmpID)
.Select(g => {
var byPres = g.OrderByDescending(x => x.DateofPresentation).ToArray();
var e = byPres.First();
e.Others = byPres.Skip(1).ToList();
return e;
})