Reputation: 936
I am trying to compare date only. The value in table is DateTime with format 2014-01-29 09:00:00.000. Please advise thank you
public static List<vwReportDate> GetDetail(string startDate, string endDate)
{
startDate = "2014-01-28";
endDate = "2014-01-28";
DateTime dtStart = Convert.ToDateTime(startDate);
DateTime dtEndDate = Convert.ToDateTime(endDate);
var entities = new DataEntiies();
var query = from c in entities.vwReportDate
where c.EventCreateDate >= dtStart && c.EventCreateDate <= dtEndDate
select c;
return query.ToList();
}
Upvotes: 0
Views: 3486
Reputation: 43036
My approach to this problem, which does not depend on EF, is to use "less than" for the end of the date range (after moving the end of the date range one day forward):
startDate = "2014-01-28";
endDate = "2014-01-28";
DateTime dtStart = Convert.ToDateTime(startDate);
DateTime dtEndDate = Convert.ToDateTime(endDate).AddDays(1);
var entities = new DataEntiies();
var query = from c in entities.vwReportDate
where c.EventCreateDate >= dtStart && c.EventCreateDate < dtEndDate
select c;
return query.ToList();
Question: why do you ignore the argument values provided by the method's caller?
Upvotes: 2
Reputation: 7517
Use the Date
property of a DateTime
struct to retrieve the "date" part.
Gets the date component of this instance.
Use it in code:
var query = from c in entities.vwReportDate
where c.EventCreateDate.Date >= dtStart && c.EventCreateDate.Date <= dtEndDate
select c;
If you are using Entity framework (as it looks like you do), you'll have to use the EntityFunctions.TruncateTime
(helper) method, because otherwise the query can't be converted.
var query = from c in entities.vwReportDate
where EntityFunctions.TruncateTime(c.EventCreateDate) >= dtStart && EntityFunctions.TruncateTime(c.EventCreateDate) <= dtEndDate
select c;
Upvotes: 1
Reputation: 125610
It looks like you're using Entity Framework and LINQ to EF. If that's true you can't use DateTime.Date
property because it's not supported in LINQ to EF. You have to use EntityFunctions.TruncateTime(myDateTime)
static method instead:
var query = from c in entities.vwReportDate
let eventDate = EntityFunctions.TruncateTime(c.EventCreateDate)
where eventDate >= dtStart && eventDate <= dtEndDate
select c;
Upvotes: 3