Entity Framework select one of each group by date
I suppose you can group your Posts rows by type and then select first content from descending ordered by date collection of that type
from row in Posts
group row by row.type
into g
select new
{
Content = (from row2 in g orderby row2.date descending select row2.content).FirstOrDefault(),
Type = g.Key
}
If you want to get the whole Posts. You can try this:
var query = Posts.GroupBy(p => p.Type)
.Select(g => g.OrderByDescending(p => p.Date)
.FirstOrDefault()
)
Or using temporary result and predicate
var tmp = posts.GroupBy(x => x.type).Select(x => new {x.Key, date = x.Max(g => g.date)).ToArray();
var filter = PredicateBuilder.False<Post>();
foreach (var item in tmp)
{
filter = filter.Or(x => x.type == item.Key && x.date == item.date);
}
var newestPosts = posts.Where(filter);
From memory, something like this should do it
var data = context.Posts.GroupBy(p => p.Type)
.Select(g => new {
Type = g.Key,
Date = g.OrderByDescending(p => p.Date)
.FirstOrDefault()
}
This would give you a new anonymous type, but you can always map it to a class