我有以下课程:
public class Order
{
public int Oid {get; set;}
public DateTime OrderDate {get; set;}
public List<OrderDetails> OrderDetails {get; set;}
}
public class OrderDetail
{
public int Oid {get; set;}
public int Price {get; set;}
public Order Order {get; set;}
public EventSchedule EventSchedule {get; set;}
}
public class EventSchedule
{
public int Oid {get; set;}
public BaseEvent BaseEvent {get; set;}
public List<OrderDetail> OrderDetails {get; set;}
}
public class BaseEvent
{
public int Oid {get; set;}
public string Name {get; set;}
public List<EventSchedule> EventSchedules {get; set;}
}
我有以下代码:
var result = Database.Orders.Where(o => o.DateTime == dateTime);
现在,我想对result
:
case "Date":
result = (sortingOrder == "desc") ? result.OrderByDescending(c => c.OrderDate) : result.OrderBy(c => c.OrderDate);
break;
// the problem is here this code not works
case "EventName":
result = (sortingOrder == "desc") ? result.All(o=>o.OrderDetails.OrderBy(d=>d.EventSchedule.BaseEvent.Name)) : ...;
break;
Order
的集合为OrderDetail
。 OrderDetail
有EventSchedule
。并且EventSchedule
有BaseEvent
。这可以通过Order
中的Name
属性对BaseEvent
对象进行排序吗?
答案 0 :(得分:2)
这个怎么样?
case "EventName":
result = results.OrderBy(
r => r.OrderDetails.Select(
od => od.EventSchedule.BaseEvent.Name).Distinct().FirstOrDefault());
//...