简体   繁体   English

基于集合属性的字典值中的OrderBy集合

[英]OrderBy Collection in Dictionary Value based on Property of the Collection

I'm having a List<Department> , in that it has Department Name , List of Employees Name and OrderBy Dicrection for List. 我有一个List<Department> ,因为它具有Department NameEmployees Name列表List的 OrderBy Dicrection Now I need to construct a Dictionary<string, ObservableCollection<Employee>> , the KEY is a Department Name and the Value is an ObservableCollection by using List<Department> . 现在,我需要使用List<Department>构造一个Dictionary<string, ObservableCollection<Employee>> ,其KEY是部门名称,而是ObservableCollection。

Expectation : I need to Sort the EmpName within the ObservableCollection<Employee> which is present in Dictionary<string, ObservableCollection<Employee>> EmpList based on sortEmpName Property which is in List<Department> DepList using LINQ 期望 :我需要使用基于LINQ的List<Department> DepList sortEmpName属性,对Dictionary<string, ObservableCollection<Employee>> EmpList存在的ObservableCollection<Employee>EmpName进行排序

I written the LINQ for this in the below Main() Function. 我在下面的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);

}

The Model Classes are 模型类是

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

The Output Screen Shot of List<Department> DepList List<Department> DepList的输出屏幕List<Department> DepList

在此处输入图片说明

The Output Screen Shot of Dictionary<string, ObservableCollection<Employee>> EmpList Dictionary<string, ObservableCollection<Employee>> EmpList的输出屏幕Dictionary<string, ObservableCollection<Employee>> EmpList

在此处输入图片说明

Expectation : I need to Sort the EmpName within the ObservableCollection<Employee> which is present in Dictionary<string, ObservableCollection<Employee>> EmpList based on sortEmpName Property which is in List<Department> DepList using LINQ 期望 :我需要使用基于LINQ的List<Department> DepList sortEmpName属性,对Dictionary<string, ObservableCollection<Employee>> EmpList存在的ObservableCollection<Employee>EmpName进行排序

This is a small part of a complex LINQ query in my project, so, I need to achieve this in LINQ. 这只是我的项目中复杂LINQ查询的一小部分,因此,我需要在LINQ中实现。 Kindly assist me. 请帮助我。

  • HR => List of Employee Names should be in Ascending HR =>员工姓名列表应按升序排列
  • iLab => List of Employee Names should be in Descending iLab =>员工姓名列表应以降序排列
  • Testing => List of Employee Names should be in original order - None (ie, No Change - Don't Sort) 测试 =>员工姓名列表应按原始顺序排列- (即,无变化-不排序)

I guess you need something like this: 我猜你需要这样的东西:

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

use OrderBy 使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM