简体   繁体   中英

How to get only the date value from DateTime and count it?

How to compare only the date from the DateTime using LINQ to Entities and then sum it with another query.

I would like to achive the following - to count the number of Type having the value "Complete" and to count the Today's date and display their sum in a textbox.

 var query_test = (from c in this.db.Documents
                   where c.Type == "Complete" && c.Date == DateTime.Now.Date
                   select c).Count();

when i run this query, I get an exception

"'System.Nullable' does not contain a definition for 'Date' and no extension method 'Date' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?) "

I have tried the below query from other similar posts, but still it doesn't work

var q1 = (this.db.Documents.Where(r => r.Date.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd") && r.Type == "Complete").Count());

kindly help

You could try this.

var query_test = (from c in this.db.Documents
                   where c.Type == "Complete" && c.Date == DateTime.Today
                   select c).Count();

if you aren't sure if there is a time element you could truncate it.

var query_test = (from c in this.db.Documents
                   where c.Type == "Complete" && EntityFunctions.TruncateTime(c.Date) == DateTime.Today
                   select c).Count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM