0

Given a table and data like

Id  Year  Code  ColB  ColC
1   2013  A     Foo   Bar
2   2014  A     Baz   Qux
3   2014  B     Jax   Fizz
4   2013  C     Buub  Baq

I wish to select the "newest" row for each code, e.g.:

Id  Year  Code  ColB  ColC
2   2014  A     Baz   Qux
3   2014  B     Jax   Fizz
4   2013  C     Buub  Baq

I see this can be done directly in SQL

select a,b 
from (
select a,b,row_number() over(partition by a order by b desc) as roworder
from myTable
) temp
where roworder = 1

https://stackoverflow.com/a/7344770/141172

Can this be expressed in Linq to Entities?

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • I'm sure that you have tried `db.MyTable.GroupBy(item=>item.Code).Select(g => g.OrderByDescending(v => v.Year).First())`, right? – Sergey Kalinichenko Jun 03 '14 at 01:52
  • @dasblinkenlight: No, I wasn't sure how to sub-select the result of GroupBy. I'll give that a shot in a few minutes. – Eric J. Jun 03 '14 at 02:04

0 Answers0