![](/img/trans.png)
[英]How can I return an IOrderedEnumerable from a Collection based on OrderBy “property”
[英]OrderBy Collection in Dictionary Value based on Property of the Collection
我有一個List<Department>
,因為它具有Department Name , Employees 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中實現。 請幫助我。
我猜你需要這樣的東西:
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.