2012-11-13 235 views
3

我有以下類別:排序母公司

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的集合。 OrderDetailEventSchedule。並且,EventScheduleBaseEvent。這是可能Order對象由Name財產在BaseEvent

回答

2

這個怎麼樣

case "EventName": 
    result = results.OrderBy(
     r => r.OrderDetails.Select(
      od => od.EventSchedule.BaseEvent.Name).Distinct().FirstOrDefault()); 
//...