0

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());
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Footabll.My.Life
  • 155
  • 2
  • 4
  • 13
  • `d.Date` doesn't return `DateTime`? Because `ConvertShamsiToMiladi` is an extension method for `string` type, not `DateTime`. – Soner Gönül Dec 31 '14 at 15:08

2 Answers2

1

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,

Bouam
  • 484
  • 2
  • 10
0

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

Community
  • 1
  • 1
Gigo
  • 3,188
  • 3
  • 29
  • 40