On the query below how can remove de time part of the datetime column to get only the distinct dates?
using (var ctx = new DBConn())
{
var q = ctx.Table.Select(r => r.datetimefield).Distinct();
}
You are stumbling over a fault with linq-to-sql or EF (I don't know which one you are using). The straight forward solution would be to use the DateTime.Date
property, however, this is unsupported in both ORMs, it doesn't map to an SQL method.
You have two solutions:
eager evaluation of the query by using ToList
:
var q = ctx.Table.ToList().Select(r => r.datetimefield.Date).Distinct();
create a field detailing only the date component in the database. This is the way I usually go as you can stay in the database and execute the query there.
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