This article is a mirror article of machine translation, please click here to jump to the original article.

View: 29994|Reply: 2

[Source] linq/ef framework Lambda expression where query for today's date

[Copy link]
Posted on 9/7/2016 1:44:20 PM | | | |


Today is September 7, 2016, I want to query today's data based on ActionDate, what should I do????

ActionDate is of the DateTime type, usually people will use the DateTime > yesterday and the DateTime < tomorrow, so

In fact, there is no problem in doing this, the Microsoft EF framework actually comes with a method:

DbFunctions.DiffDays method

When used as part of a LINQ to Entities query, this method calls the canonical DiffDays EDM function to calculate the number of days between two dates/times.




Through this code, we can query two pieces of data, as follows:




In fact, it is the data we want, let's take a look at what SQL statements the database generates.









Previous:If your project references the old entity framework and you are using the new framework, close the wizard to select the new framework
Next:Unable to open physical file C:\Databases\xxx.mdf. OS error 5: "5 (Access denied. )
Posted on 9/7/2016 1:48:06 PM |
Thanks for solving my problem very well
 Landlord| Posted on 9/21/2018 11:25:10 AM |
Get this month's data var monRet= ret. Where(m => DbFunctions.DiffMonths(m.Test, DateTime.Now) == 0);
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com