簡體   English   中英

基於集合屬性的字典值中的OrderBy集合

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

我有一個List<Department> ,因為它具有Department NameEmployees Name列表List的 OrderBy Dicrection 現在,我需要使用List<Department>構造一個Dictionary<string, ObservableCollection<Employee>> ,其KEY是部門名稱,而是ObservableCollection。

期望 :我需要使用基於LINQ的List<Department> DepList sortEmpName屬性,對Dictionary<string, ObservableCollection<Employee>> EmpList存在的ObservableCollection<Employee>EmpName進行排序

我在下面的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<Department> DepList的輸出屏幕List<Department> DepList

在此處輸入圖片說明

Dictionary<string, ObservableCollection<Employee>> EmpList的輸出屏幕Dictionary<string, ObservableCollection<Employee>> EmpList

在此處輸入圖片說明

期望 :我需要使用基於LINQ的List<Department> DepList sortEmpName屬性,對Dictionary<string, ObservableCollection<Employee>> EmpList存在的ObservableCollection<Employee>EmpName進行排序

這只是我的項目中復雜LINQ查詢的一小部分,因此,我需要在LINQ中實現。 請幫助我。

  • HR =>員工姓名列表應按升序排列
  • iLab =>員工姓名列表應以降序排列
  • 測試 =>員工姓名列表應按原始順序排列- (即,無變化-不排序)

我猜你需要這樣的東西:

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

使用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