gpt4 book ai didi

entity-framework - 使用 LINQ 和 Entity Framework 添加前导零

转载 作者:行者123 更新时间:2023-12-02 11:25:01 24 4
gpt4 key购买 nike

我尝试通过 Linq 在我的 ASP MVC 应用程序中添加前导零:

int length = 4;
IEnumerable<object> query = [...]
select new
{
Seq = a.Seq.ToString("D" + length),
}).OrderBy(a =>a.Seq).ToList();

..但我收到以下错误:
Additional information: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

这样做的正确方法是什么?

最佳答案

我想 String.PadLeft支持(至少在 Linq-To-Sql 中它有效):

Seq = a.Seq.PadLeft(length, '0')}

如果这不起作用(我无法测试),您可以使用 SqlFunctions.Replicate :
Seq = SqlFunctions.Replicate("0", length - a.Seq.ToString().Length) + a.Seq

(长度计算需要修改,希望你能理解)

关于entity-framework - 使用 LINQ 和 Entity Framework 添加前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994275/

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