Footabll.My.Life
Footabll.My.Life

Reputation: 155

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

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());
}

Upvotes: 0

Views: 3341

Answers (2)

Gigo
Gigo

Reputation: 3264

Using LINQ to Entities your query is converted to SQL and ConvertShamsiToMiladi is not implemented in SQL. You either select with a proper statement and convert the date afterwards, or do the whole selection on the client side, i.e. with _paymentService.AsEnumerable().GetMany(...).

"LINQ to Entities does not recognize ..." has been discussed many times here:
LINQ to Entities does not recognize the method 'System.String ToBase64String(Byte[])' method,
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

Upvotes: 0

Bouam K
Bouam K

Reputation: 463

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 :

var _48hoursAgoPayments =_paymentService.AsEnumerable().GetMany(d => d.Date.ConvertShamsiToMiladi() >= _48hoursAgoDate)

Regards,

Upvotes: 1

Related Questions