【问题标题】:OrderBy Collection in Dictionary Value based on Property of the Collection基于集合属性的字典值中的 OrderBy 集合
【发布时间】:2016-04-04 11:12:43
【问题描述】:

我有一个List<Department>,因为它有部门名称员工姓名列表OrderBy Direction列表。现在我需要构造一个Dictionary<string, ObservableCollection<Employee>>KEY 是部门名称,Value 是使用List<Department> 的 ObservableCollection。

期望:我需要根据sortEmpName 中的@987654332 中的Dictionary<string, ObservableCollection<Employee>> EmpList 属性对ObservableCollection<Employee> 中的EmpName 进行排序 @ 使用 LINQ

我在下面的 Main() 函数中为此编写了 LINQ。

void Main()
{
    List<Department> DepList = new List<Department>()
    {
        new Department() { 
            DepName = "HR", 
            sortEmpName = FilterListSortDirection.SortDirection.Ascending, 
            EmpName = new List<string>() {"Raj", "Baba"}
        },
        new Department() {
            DepName = "iLab",
            sortEmpName = FilterListSortDirection.SortDirection.Descending,
            EmpName = new List<string>() {"Emma", "Kaliya"}
        },
        new Department() {
            DepName = "Testing",
            sortEmpName = FilterListSortDirection.SortDirection.None,
            EmpName = new List<string>() {"Supriya", "Billa"}
        }
    };


    Dictionary<string, ObservableCollection<Employee>> EmpList = DepList.Select(m => 
                                new {
                                    Dep = m.DepName,
                                    EmpCollection = new ObservableCollection<Employee>(
                                                            m.EmpName.Select(k =>
                                                                new Employee() { EmpName = k, IsChecked = true }).ToList())
                                }
                            ).ToDictionary(x => x.Dep, x => x.EmpCollection);

}

模型类是

public class Employee
{
    public string EmpName { get; set; }
    public bool IsChecked { get; set; }
}

public class Department
{
    public string DepName { get; set; }
    public FilterListSortDirection.SortDirection sortEmpName { get; set; }
    public List<string> EmpName { get; set; }
}

public class FilterListSortDirection
{
    public enum SortDirection
    {
        None,
        Ascending,
        Descending
    }
}

List&lt;Department&gt; DepList的输出截图

Dictionary&lt;string, ObservableCollection&lt;Employee&gt;&gt; EmpList的输出截图

期望:我需要根据sortEmpName 中的sortEmpName 属性对ObservableCollection&lt;Employee&gt; 中的ObservableCollection&lt;Employee&gt; 中的EmpName 进行排序 @ 使用 LINQ

这是我项目中复杂 LINQ 查询的一小部分,因此,我需要在 LINQ 中实现这一点。请帮助我。

  • HR => 员工姓名列表应为升序
  • iLab => 员工姓名列表应按降序
  • 测试 => 员工姓名列表应按原始顺序 - (即,无更改 - 不排序)

【问题讨论】:

    标签: c# linq sorting dictionary


    【解决方案1】:

    我猜你需要这样的东西:

    var EmpList = DepList.ToDictionary(p => p.DepName, p =>
        {
            var empList = p.EmpName.Select(k => new Employee() { EmpName = k, IsChecked = true });
            if (p.sortEmpName == FilterListSortDirection.SortDirection.Ascending)
            {
                empList = empList.OrderBy(q => q.EmpName);
            }
            else if (p.sortEmpName == FilterListSortDirection.SortDirection.Descending)
            {
                empList = empList.OrderByDescending(q => q.EmpName);
            }
            return new ObservableCollection<Employee>(empList.ToList());
        });
    

    【讨论】:

      【解决方案2】:

      使用 OrderBy

      Dictionary<string, ObservableCollection<Employee>> EmpList = DepList.Select(m => new
      {
          Dep = m.DepName,
          EmpCollection = 
          m.sortEmpName == SortDirection.Ascending ? 
              new ObservableCollection<Employee>(m.EmpName.Select(k => new Employee {EmpName = k, IsChecked = true}).ToList().OrderBy(emp => emp.EmpName)) :
          m.sortEmpName == SortDirection.Descending ?
              new ObservableCollection<Employee>(m.EmpName.Select(k => new Employee { EmpName = k, IsChecked = true }).ToList().OrderByDescending(emp => emp.EmpName)) :
              new ObservableCollection<Employee>(m.EmpName.Select(k => new Employee { EmpName = k, IsChecked = true }).ToList())
      }).ToDictionary(x => x.Dep, x => x.EmpCollection);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-12
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        相关资源
        最近更新 更多