I'm trying to write a query where it only needs to ping the db once. Currently I have:
var postQuery = context.Posts.Where(...);
var min = postQuery.Min(d => d.Start);
var max = postQuery.Max(d => d.End);
Is there a way to combine this into a single select statement?
Ok, it's a bit tricky but here it is:
var result = context.Posts.Where(...)
.GroupBy(p => 1) // Group by a constant to get a single row result
.Select(g => new
{
MinStart = g.Min(p => p.Start),
MaxEnd = g.Max(p => p.End)
})
.FirstOrDefault();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With