C # DateTime to minutes from linq

I am trying to compare two DateTimes in minutes from DateTime.Now and SQL records. I see that the problem is that the ticks are a little disabled due to the fact that they are returned from the database. However, I can’t get it right.

This is my DateTime to capture

 DateTime toGrab = DateTime.Now.AddMinutes(10) .AddSeconds(-DateTime.Now.Second) .AddMilliseconds(-DateTime.Now.Millisecond); 

And there is my linq expression.

 cc.DBAppointments.Where(c => c.StartDate == toGrab && c.Approved == 1).ToList(); 

Any tips?

+8
c # datetime linq
source share
4 answers

I assume that you are using the Entity Framework for this. If you just need to compare the date and time to minutes, you can use System.Data.Entity.DbFunctions to access canonical functions in linq-to-entity. You can use this, for example:

 cc.DBAppointments .Where(c => DbFunctions.DiffMinutes(c.StartDate,DateTime.Now) == 0 && c.Approved == 1) .ToList(); 

That way, you have access to sql datetime advanced data mapping, and you don't need to do the tricks by adding seconds / milliseconds to your DateTime.Now . This function will ignore less and less minutes .

+6
source share

You make several references to DateTime.Now , which may have a different meaning each time. The following should work better:

 var now = DateTime.Now; var toGrab = now.Date.AddHours(now.Hour).AddMinutes(now.Minute + 10); 

Note that to get a clear “minute top” adding hours and minutes to a date will work better than trying to remove seconds and milliseconds because there are more details in DateTime than milliseconds.

+4
source share

Change

 DateTime toGrab = DateTime.Now.AddMinutes(10) .AddSeconds(-DateTime.Now.Second) .AddMilliseconds(-DateTime.Now.Millisecond); 

For

 DateTime now = DateTime.Now; DateTime toGrab = now.AddMinutes(10) .AddSeconds(-now.Second) .AddMilliseconds(-now.Millisecond); 

In another note, is there a reason you would need to schedule an appointment for the exact millisecond? Does he seem very prone to missing entries?

Edit

In addition, in your database, the value 2015-12-10 10:33:48.373 differs from 2015-12-10 10:33:48.374 , although they are at a distance of 1 millisecond. If you want a wider range, you can do c.StartDate >= startDate && c.StartDate <= endDate && c.Approved == 1

+1
source share

You can do a comparison of TimeSpan objects , which means you can do it like this:

 TimeSpan oneMinute = new TimeSpan(0, 1, 0); cc.DBAppointments.Where(c => DateTime.Now.AddMinutes(10).Subtract(c.StartDate) < oneMinute && c.Approved == 1).ToList(); 

Of course, if c.StartDate less than 9 minutes in the future, subtraction will have a result <-1 minutes. I am not sure your goal is within 1 minute. In this case, you can add .Duration() to your equation, which will return the absolute value of TimeSpan:

 cc.DBAppointments.Where(c => DateTime.Now.AddMinutes(10).Subtract(c.StartDate).Duration() < oneMinute && c.Approved == 1).ToList(); 
+1
source share

All Articles