c# - Way to create a tree from two lists with a matching ID -


let`s have following 2 models :

public class order {     public guid id { get; set; }     public string name { get; set; }     public string location { get; set; }     public string street { get; set; }     public ilist<appointment> appointments { get; set; } }  public class appointment {     public guid id {get; set;}     public datetime startdate {get; set;}     public datetime enddate {get; set;}     public guid orderid { get; set; }  } 

i have list order items (all list of appointments empty) , list appointments , don`t know how match elements 2 lists in order obtain order object corresponding appointments (based on orderid).

how can in efficient manner ? don`t want iterate through orders, , assign corresponding appointments..

one way "group join" 2 lists , project new collection:

var neworders =      o in orders     join in appointments       on o.id equals a.orderid        g     select new order     {        id = o.id,        name = o.name,        location = o.location,        street = o.street,        appointments = g.tolist()     }; 

obviously creates new order objects - if not desirable option loop through of orders , "attach" matching appointments - using tolookup pre-group them:

var groups = appointments.tolookup(a => a.orderid);  foreach(var o in orders)     o.appointments = groups[o.id].tolist(); 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -