LINQ to Entities 无法识别我的方法
LINQ to Entities does not recognize my method
我想在 LINQ 中将日期和时间转换为波斯语 select 但 linq 无法识别我的方法:
LINQ to Entities does not recognize the method 'System.String toPersianDateTime(System.DateTime)' method, and this method cannot be translated into a store expression.
如何将我的方法更改为与 LINQ 兼容?
我的方法:
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;
}
还有我的 LINQ 代码:
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 });
EF 无法将您的自定义方法转换为 SQL。您可以注入 .AsEnumerable()
调用以将基础上下文从 EF 更改为 Linq-to-Objects:
var result = (from ord in db.vw_orders
where ord.uid == user.id
orderby ord.order_date descending select ord
)
.AsEnumerable()
.Select(o => new { o.id,
date = Tools.toPersianDateTime((DateTime)o.order_date),
o.is_final,
o.status,
o.image_count,
o.order_count,
o.total_price }
);
我想在 LINQ 中将日期和时间转换为波斯语 select 但 linq 无法识别我的方法:
LINQ to Entities does not recognize the method 'System.String toPersianDateTime(System.DateTime)' method, and this method cannot be translated into a store expression.
如何将我的方法更改为与 LINQ 兼容?
我的方法:
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;
}
还有我的 LINQ 代码:
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 });
EF 无法将您的自定义方法转换为 SQL。您可以注入 .AsEnumerable()
调用以将基础上下文从 EF 更改为 Linq-to-Objects:
var result = (from ord in db.vw_orders
where ord.uid == user.id
orderby ord.order_date descending select ord
)
.AsEnumerable()
.Select(o => new { o.id,
date = Tools.toPersianDateTime((DateTime)o.order_date),
o.is_final,
o.status,
o.image_count,
o.order_count,
o.total_price }
);