LINQ OrderBy on inner object's property -
below hierarchies , required function
public class friend { public string name { get; set; } public list<message> messages { get; set; } } public class message { public string text { get; set; } public datetime time { get; set; } }
now required function:
public list<string> whatsapp(list<friend> friends) { throw new notimplementedexception(); }
i need list of friend names in descending order of there message time stamp. whats app or other im matter.
i getting feeling can done in 1 or 2 lines using linq since new linq, unable drill down problem.
thanks in advance help.
if idea order last (i.e. max) message timestamp, following should job:
return friends.orderbydescending(f => f.messages.max(m => (datetime?)m.time)) .select(f => f.name) .tolist();
casting datetime?
needed avoid exception when there no messages friend.
in general when need order parent having multiple children based on children properties, should aggregate value (like sum
, count
, min
, max
etc.).
Comments
Post a Comment