I want to convert date and time to Persian in LINQ select but linq can not recognize my method :
LINQ to Entities does not recognize the method 'System.String toPersianDateTime(System.DateTime)' method, and this method cannot be translated into a store expression.
How can I change my method to LINQ compatible?
My method :
public static string toPersianDateTime(DateTime dt)
{
PersianCalendar pc = new PersianCalendar();
string pDateTime = pc.GetYear(dt).ToString() + "/" + pc.GetMonth(dt).ToString() + "/" + pc.GetDayOfMonth(dt).ToString() + " ";
pDateTime += pc.GetHour(dt) + ":" + pc.GetMinute(dt) + ":" + pc.GetSecond(dt);
return pDateTime;
}
And my LINQ code :
var result = (from ord in db.vw_orders
where ord.uid == user.id
orderby ord.order_date descending
select new { ord.id,
date = Tools.toPersianDateTime((DateTime)ord.order_date),
ord.is_final,
ord.status,
ord.image_count,
ord.order_count,
ord.total_price });
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…