Reputation: 421
Hi i have one view called DailyVisitReport. In that view i have two field called FromDate and toDate. if i select the FromDate and ToDate depends upon the date it shows the table with data in the same view (like partial view).
Now FromDate and ToDate date Format in jquery is "dd-M-y"(01-Mar-16)
In Db also i changed the Date format to (01-Mar-16 STRING).
My Model(VisitorsViewModel)
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
public ICollection<View_VisitorsForm> Visits { get; set; }
My Controller Code
public ActionResult GetDatesfromFromDateToDate(string fromDate, string toDate)
{
CultureInfo provider = CultureInfo.InvariantCulture;
var fromdt = Convert.ToDateTime(fromDate);
var todt = Convert.ToDateTime(toDate);
List<View_VisitorsForm> VisitCount = (from v in db.View_VisitorsForm where DateTime.ParseExact(v.VisitingDate,"dd-M-y", provider) >= fromdt && DateTime.ParseExact(v.VisitingDate,"dd-M-y",provider) <= todt select v).ToList();
VisitorsViewModel visitotsVM = new VisitorsViewModel();
visitotsVM.Visits = VisitCount;
return PartialView("_Visitors", visitotsVM);
}
Now I select the FromDate
and ToDate
it show the error which is mention below
LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method
I cant able to convert that v.VisitingDate
to DateFormat
from string format .
I tried Convert.ToDateTime
and Convert.ToParse
all things. still I'm getting error. please any one understand my issue and help me to resolve this issue.
Upvotes: 2
Views: 2157
Reputation: 2814
All the operations inside a Linq to Entities query are converted to sql. DateTime.Parse cannot be translated to sql thus the error.
Perform the parse outside the where
to solve specifically that problem but, more generally, you should make VisitingDate
a DateTime property too.
Upvotes: 3
Reputation: 5155
The property VisitingDate
should be of type DateTime
to avoid these struggles. Do not compare datetime values by using string mishmash, use DateTime
types.
The where condition would then look like
where v.VisitingDate.HasValue ?
v.VisitingDate.Value >= fromdt && v.VisitingDate.Value <= todt
:
... // put a condition here when visiting date has no value in DB
Also heres a list of supported / unsupported methods in LINQ to SQL, i assume the same goes for LINQ to Entities: https://msdn.microsoft.com/en-us/library/bb386970.aspx
Therefore you cannot use Parse....
Upvotes: 1