-9

I have the following table:

------------------------------------
userid | testid | date     | result
1      | 1      | 18-01-01 | 1
1      | 1      | 18-01-09 | 6
1      | 3      | 18-01-09 | 5
1      | 3      | 18-01-10 | 2

Now i need to get 2 rows using Entity: The following:

------------------------------------
userid | testid | date     | result
1      | 1      | 18-01-09 | 6
1      | 3      | 18-01-10 | 2

I need a LINQ query that returns the latest result of each testid in the database. The userID/testID is the group. What is the best and fastest way to get this information?

Thanks,

Joey Erdogan
  • 184
  • 1
  • 13

1 Answers1

2

Just GroupBy() the testid and select the latest of each group

var result = ctx.Items.GroupBy(x => x.testid)
                      .Select(x => x.OrderByDescending(y => y.date).First());
fubo
  • 44,811
  • 17
  • 103
  • 137