【问题标题】:Move duplicates of a list within the list c#在列表c#中移动列表的重复项
【发布时间】:2014-09-08 12:24:07
【问题描述】:

我有以下清单。

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 中实现这一点吗?

【问题讨论】:

  • 您可以与我们分享解决此问题的任何尝试吗?也请发布有效代码,您问题中的代码sn-ps没有机会编译。
  • 请重新考虑这一点,您在这里似乎有一名员工有三个演示日期,但是如果您有一名员工负责两名具有相同 ID 和姓名的员工,您的结构会显示什么与他们自己的演示日期 - 然后这会变得混乱,因为相同的排序逻辑可以应用于这些子员工
  • 应该如何编译?您还没有为new list&lt;Employee&gt;(无论list 是什么)指定属性。
  • @FrédéricHamidi 请查看我的编辑。抱歉,错过了该物业。我需要这种方式

标签: c# linq lambda


【解决方案1】:

您需要向Employee 添加一个属性(我称之为Others)来存储列表

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

查看演示https://dotnetfiddle.net/kRiMds

【讨论】:

    【解决方案2】:

    这是一个代码解决方案:

        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();
    

    【讨论】:

    • 可行,但上面的解决方案似乎更好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多