Linq request for a specific date

In the question below, how to remove the datetime column part time to get only individual dates?

using (var ctx = new DBConn())
{
  var q = ctx.Table.Select(r => r.datetimefield).Distinct();
}
+3
source share
2 answers

You are facing an error in linq-to-sql or EF (I don't know which one you are using). A direct solution would be to use a property DateTime.Date, however this is not supported in both ORMs, it does not map to the SQL method.

You have two solutions:

  • Simple query assessment with ToList:

    var q = ctx.Table.ToList().Select(r => r.datetimefield.Date).Distinct();
    
  • Create a field that only details the date component in the database. So I usually go because you can stay in the database and execute the query there.

+11
source

, r.datetimefield DateTime, DateTime.Date, :

var q = ctx.Table.Select(r => r.datetimefield.Date).Distinct();
+4

All Articles