.net - LINQ Grouping: Select row instead of key? -


i won't able explain enough, here goes...

i have table similar this:

id | foreign_key_id | a_number | some_other_info 1      1                100          red 2      1                200          blue 3      1                300          orange 4      2                100          green 5      2                200          yellow 6      3                100          brown 

i want max "a_number" particular "foreign_key_id" in table, return rest of record have info "some_other_info" column.

i've found can accomplish via query this:

from x in this_table group x x.foreign_key_id g orderby g.max(x => x.a_number) descending select g.where (x => x.foreign_key_id == g.key).orderbydescending (x => x.a_number).first() 

edit: or possibly more concise query:

from x in this_table group x x.foreign_key_id g let maxanumber = g.max(x => x.a_number) orderby maxanumber descending select g.where(x => x.a_number == maxanumber).first() 

although i'm not quite sure record take if 1 foreign_key_id had 2 records same "a_number" value (which possible in table referring to, need take recent one).

which in example believe return:

id | foreign_key_id | a_number | some_other_info 3      1                300          orange 5      2                200          yellow 6      3                100          brown 

it seems there has way in easier fashion (that makes faster query 1 came with), can't figure out how.

thanks in advance.

the following works. can order inside group:

from x in this_table group x x.foreign_key_id g select g.orderbydescending(y => y.a_number).first(); 

taking recent record when a_numbers equal simple adding orderby query.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -