[英]LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method
error : LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method, and this method cannot be translated into a store expression
错误:LINQ to Entities无法识别方法'System.DateTime ConvertShamsiToMiladi(System.String)'方法,并且该方法无法转换为商店表达式
How do I fix it? 我如何解决它?
my codes : 我的代码:
var _48hoursAgoDate = DateTime.Now.AddDays(-2);
var _48hoursAgoPayments =_paymentService.GetMany(d => d.Date.ConvertShamsiToMiladi() >= _48hoursAgoDate)
extension method : 扩展方法:
public static DateTime ConvertShamsiToMiladi(this string source)
{
var date = source.Split('/');
return new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]), new PersianCalendar());
}
You have this message because Linq doesent know how to translate your method to SQL You should convert it to Linq to object first by using .AsEnumerable() it should look like this : 您收到此消息是因为Linq确实知道如何将您的方法转换为SQL您应该首先使用.AsEnumerable()将其转换为Linq成为对象,它应该看起来像这样:
var _48hoursAgoPayments =_paymentService.AsEnumerable().GetMany(d => d.Date.ConvertShamsiToMiladi() >= _48hoursAgoDate)
Regards, 问候,
Using LINQ to Entities your query is converted to SQL and ConvertShamsiToMiladi
is not implemented in SQL. 使用LINQ to Entities,您的查询将转换为SQL,而
ConvertShamsiToMiladi
未在SQL中实现。 You either select with a proper statement and convert the date afterwards, or do the whole selection on the client side, ie with _paymentService.AsEnumerable().GetMany(...)
. 您可以使用适当的语句选择并转换日期,或者在客户端进行整个选择,即使用
_paymentService.AsEnumerable().GetMany(...)
。
"LINQ to Entities does not recognize ..." has been discussed many times here: 这里多次讨论“ LINQ to Entities无法识别...”:
LINQ to Entities does not recognize the method 'System.String ToBase64String(Byte[])' method, LINQ to Entities无法识别方法'System.String ToBase64String(Byte [])'方法,
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法'System.String ToString()',并且该方法无法转换为商店表达式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.