c# - What is the fastest way to get the last elements of a list by ID with Datetime? -


i have huge list<myobject>. myobject had property named timestamp (datetime?) , code (string). need lasts objects timestamp each code.

for example following input :

code|   | b   | timestamp a1  | abc | def | fri 7 mar 2014  b2  | abc | ghi | thu 6 mar 2014  a1  | def | ghi | mon 10 mar 2014  b2  | def | ghi | fri 7 mar 2014  c3  | ghi | jkl | fri 7 mar 2014  

i need :

code|   | b   | timestamp a1  | def | ghi | mon 10 mar 2014  b2  | def | ghi | fri 7 mar 2014  c3  | ghi | jkl | fri 7 mar 2014  

in order did following function :

private list<myobject> getlastsobjects(list<myobject> input) {     var output = input.where(x => x.timestamp == x.where(y => y.code == y.code)                                                                .max(tmp => tmp.timestamp))                       .tolist();     return output; } 

it works have bad performance huge input...

do know how can optimized function ? fastest way last elements timestamp each code ?

one way group objects code, reverse sort each group timestamp , first result of each group:

var output = input     .groupby(o => o.code)     .select(g1 => g1.orderbydescending(g2 => g2.timestamp).first())     .tolist(); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -