LINQ Using Max() to select a single row

前端 未结 6 1434
轻奢々
轻奢々 2020-12-02 16:13

I\'m using LINQ on an IQueryable returned from NHibernate and I need to select the row with the maximum value(s) in a couple of fields.

I\'ve simplified the bit tha

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 16:57

    You can group by status and select a row from the largest group:

    table.GroupBy(r => r.Status).OrderByDescending(g => g.Key).First().First();
    

    The first First() gets the first group (the set of rows with the largest status); the second First() gets the first row in that group.
    If the status is always unqiue, you can replace the second First() with Single().

提交回复
热议问题