【发布时间】:2020-07-26 21:29:46
【问题描述】:
【问题讨论】:
标签: c# performance entity-framework asp.net-core entity-framework-6
【问题讨论】:
标签: c# performance entity-framework asp.net-core entity-framework-6
如果 Trans 表示类似 NameValue 对的集合,其中一个类似于 EmployeeStartDate:
employees.OrderBy(x => x.Trans.Single(t => t.Name == "EmployeeStartDate").Date));
这只是一个示例,并假设 Trans 集合始终包含必需的匹配条目。
如果 Trans 只是日期的集合,那么您需要决定使用哪个日期。例如最小或最大日期。例如,按最早日期订购:
employees.OrderBy(x => x.Trans.Min(t => t.Date));
【讨论】:
除了@Steve Py给出的答案,你还可以先选择结果再对字段进行排序你想要的。
以下是演示:
employees = (employees.Select(d => new Employee
{
Name= d.Name,
Trans= d.Trans.OrderBy(e=>e.Date).ToList()
})).ToList();
型号:
public class Employee
{
[Key]
public string Name { get; set; }
public ICollection<Trans> Trans{ get; set; }
}
public class Trans
{
public int ID { get; set; }
public DateTime Date { get; set; }
}
【讨论】: