gpt4 book ai didi

c# - LINQ to Entities 无法识别我的方法

转载 作者:行者123 更新时间:2023-11-30 20:20:01 26 4
gpt4 key购买 nike

我想在 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。您可以注入(inject) .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 }
);

关于c# - LINQ to Entities 无法识别我的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37662117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com