Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare DateTime Objects in EFCore 2.1.1 and .NET Core 2.1

I am trying to select data using EFCore 6.0 in .NET Core 2.1 trying to check whether a datetime column is less than DateTime.Now.

I have tried using DbFunction.DiffMinutes() but is of no use as I am getting no such function in the DbFunction class.

Any idea?

like image 242
Melwyn Lobo Avatar asked Dec 08 '22 14:12

Melwyn Lobo


1 Answers

Add Microsoft.EntityFrameworkCore v2.1.1 from Nuget Packages. Then, use SqlServerDbFunctionsExtensions class for getting Date comparison. Do the following what I have done in my application. I have found difference in minutes between Date1 and Date2:

DbFunctions dfunc = null;
IQueryable<TableName> lQueryableTableName = lObjInitDbContext.gObjServerAuditHistoryDbContext
   .ServerAuditHistorys
   .Where(x => SqlServerDbFunctionsExtensions
       .DateDiffMinute(dfunc, Convert.ToDateTime(Date1), Convert.ToDateTime(Date2)) < 1);

Hope this will help you!!!

like image 71
Saurabh Gupta Avatar answered Jan 02 '23 17:01

Saurabh Gupta