Linq where clause compare only date value without time value
There is also EntityFunctions.TruncateTime
or DbFunctions.TruncateTime
in EF 6.0
How to compare only date components from DateTime in EF?
NOTE: at the time of writing this answer, the EF-relation was unclear (that was edited into the question after this was written). For correct approach with EF, check Mandeeps answer.
You can use the DateTime.Date
property to perform a date-only comparison.
DateTime a = GetFirstDate();
DateTime b = GetSecondDate();
if (a.Date.Equals(b.Date))
{
// the dates are equal
}
How to compare DateTime without time via LINQ?
Just use DateTime.Today
property to take the current date. There's no need to truncate t.StartDate
in this case (and doing so may incur performance penalties).
var today = DateTime.Today;
var q = db.Games.Where(t => t.StartDate >= today)
.OrderBy(t => t.StartDate);
Note that I've explicitly evaluated DateTime.Today
once so that the query is consistent - otherwise each time the query is executed, and even within the execution, Today
could change, so you'd get inconsistent results. For example, suppose you had data of:
Entry 1: March 8th, 8am
Entry 2: March 10th, 10pm
Entry 3: March 8th, 5am
Entry 4: March 9th, 8pm
Surely either both entries 1 and 3 should be in the results, or neither of them should... but if you evaluate DateTime.Today
and it changes to March 9th after it's performed the first two checks, you could end up with entries 1, 2, 4.
Of course, using DateTime.Today
assumes you're interested in the date in the local time zone. That may not be appropriate, and you should make absolutely sure you know what you mean. You may want to use DateTime.UtcNow.Date
instead, for example. Unfortunately, DateTime
is a slippery beast...
EDIT: You may also want to get rid of the calls to DateTime
static properties altogether - they make the code hard to unit test. In Noda Time we have an interface specifically for this purpose (IClock
) which we'd expect to be injected appropriately. There's a "system time" implementation for production and a "stub" implementation for testing, or you can implement it yourself.
You can use the same idea without using Noda Time, of course. To unit test this particular piece of code you may want to pass the date in, but you'll be getting it from somewhere - and injecting a clock means you can test all the code.
How to compare date time in an anonymous entity type using LINQ. Entities does not recognize the method ToDateTime
I guess the exception you're getting is something like "LINQ Entities doesn't know the Convert.ToDateTime()"? It's totaly normal, because Entity Framework is trying to translate your expression into a SQL Query, but Convert.ToDateTime doesn't exists in SQL (or not that way...). So you have to create your DateTime before passing it to your query.
Try something like that:
var myDate = Convert.ToDateTime("2018-06-11 00:00:00.000");
var startResults = Results.Where(r => r.GamingDay.Value == myDate);
And it should do the tricks.
A little explanation: with that way, you're passing to EF a value with a known type (here it's DateTime), not a function, so it's "easier" for EF to translate your expression into a SQL query.
How can i compare date without time part in linq?
A Nullable<T>
object stores the value of T
in the .Value
property, and has a handy .HasValue
property to let you know if it's not null. So if you're comparing a DateTime?
with a DateTime
, you can do something like:
if (!request.reconciliationDate.Equals(DateTime.MinValue))
filterResult = filterResult
.Where(rd =>
rd.AdjustmentDate.HasValue &&
rd.AdjustmentDate.Value.Date <= request.reconciliationDate.Date)
.ToList();
C# - Compare DateTime without Time on LINQ?
You can do like this
.Where(a => a.Time >= date.From() && a.Time <= date.To())
Using Helper Methods
public static DateTime From(this DateTime value)
{
return new DateTime((value.Year, value.Month, value.Day);
}
public static DateTime To(this DateTime value)
{
return new DateTime(value.Year, value.Month, value.Day, 23, 59, 59);
}
Related Topics
The Type Must Be a Reference Type in Order to Use It as Parameter 'T' in the Generic Type or Method
Retrieve System Uptime Using C#
The Input Is Not a Valid Base-64 String as It Contains a Non-Base 64 Character
Why Do We Need the New Keyword and Why Is the Default Behavior to Hide and Not Override
Raise Event Thread Safely - Best Practice
X509Certificate Constructor Exception
Console.Writeline and Generic List
How to Reconcile Idisposable and Ioc
How to Round to the Nearest Whole Number in C#
What Is Point of Ssl If Fiddler 2 Can Decrypt All Calls Over Https
Handling Exceptions, Is This a Good Way
Linq to SQL Multiple Tables Left Outer Join
Anonymous Type Result from SQL Query Execution Entity Framework
What Is an MVChtmlstring and When Should I Use It