Reputation: 1694
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
Upvotes: 0
Views: 220
Reputation: 17680
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();
Upvotes: 2