[英]LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method error in MVC5
Hi i have one view called DailyVisitReport . 嗨,我有一个名为DailyVisitReport的视图。 In that view i have two field called FromDate and toDate .
在该视图中,我有两个名为FromDate和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).
如果我选择FromDate和ToDate取决于它在同一视图中显示具有数据的表的日期(如局部视图)。
Now FromDate and ToDate date Format in jquery is "dd-My"(01-Mar-16) 现在FromDate和ToDate日期jquery中的格式是“dd-My”(01-Mar-16)
In Db also i changed the Date format to (01-Mar-16 STRING). 在Db中我也将日期格式更改为(01-Mar-16 STRING)。
My Model(VisitorsViewModel) 我的模型(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 现在我选择
FromDate
和ToDate
它显示下面提到的错误
LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method
LINQ to Entities无法识别方法'System.DateTime ParseExact(System.String,System.String,System.IFormatProvider)'方法
I cant able to convert that v.VisitingDate
to DateFormat
from string format . 我无法从字符串格式将
v.VisitingDate
转换为DateFormat
。
I tried Convert.ToDateTime
and Convert.ToParse
all things. 我尝试了
Convert.ToDateTime
和Convert.ToParse
所有的东西。 still I'm getting error. 我还是得错了。 please any one understand my issue and help me to resolve this issue.
请任何人了解我的问题,并帮助我解决这个问题。
All the operations inside a Linq to Entities query are converted to sql. Linq to Entities查询中的所有操作都将转换为sql。 DateTime.Parse cannot be translated to sql thus the error.
DateTime.Parse无法转换为sql因此出错。
Perform the parse outside the where
to solve specifically that problem but, more generally, you should make VisitingDate
a DateTime property too. where
具体解决该问题的where
之外执行解析,但更一般地说,您也应该将VisitingDate
设为DateTime属性。
The property VisitingDate
should be of type DateTime
to avoid these struggles. VisitingDate
属性应该是DateTime
类型,以避免这些困难。 Do not compare datetime values by using string mishmash, use DateTime
types. 不要使用字符串mishmash比较日期时间值,请使用
DateTime
类型。
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 还有一个LINQ to SQL中支持/不支持的方法列表,我假设LINQ to Entities也是如此: https : //msdn.microsoft.com/en-us/library/bb386970.aspx
Therefore you cannot use Parse.... 因此你不能使用Parse ....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.