This is based on a previous question by Todilo.
The following accepted answer works great except I am required to return all records where type is null in addition to the latest for each type:
var query = Posts.GroupBy(p => p.Type)
.Select(g => g.OrderByDescending(p => p.Date)
.FirstOrDefault()
)
The scenario is as follows:
+----+--------------------------+-------+------------+
| id | content | type | date |
+----+--------------------------+-------+------------+
| 0 | Some text | TypeA | 2013-04-01 |
| 1 | Some older text | TypeA | 2012-03-01 |
| 2 | Some even older texttext | TypeA | 2011-01-01 |
| 3 | Sample | | 2013-02-24 |
| 4 | A dog | TypeB | 2013-04-01 |
| 5 | And older dog | TypeB | 2012-03-01 |
| 6 | An even older dog | TypeB | 2011-01-01 |
| 7 | Another sample | | 2014-03-06 |
| 8 | Test | | 2015-11-08 |
+----+--------------------------+-------+------------+
The result should be
Some text | TypeA
Sample |
A dog | TypeB
Another sample |
Test |