[英]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.