gpt4 book ai didi

c# - LinQ 如何将 1 之类的整数更改为字符串 001

转载 作者:行者123 更新时间:2023-11-30 13:25:56 24 4
gpt4 key购买 nike

我的数据库中有一个包含四列的表:

string: year
string: weeknr
int: number

在其他表中,我将这些列作为字符串组合到 yywwnnn 中。

数字列是标识列。

现在我想从一个表中检索一些记录,我想将这些记录与提到的表连接起来。

类似于:

from R in db.Requests
join RN in db.RequestNumbers on R.RequestNumber equals (RN.Year + RN.Weeknr + RN.Number)

当然 RN.Number 是一个整数,我需要它是一个 3 位数的字符串。

所以:
16 07 1 ==> 1607001
16 07 10 ==> 1607010
16 07 100 ==> 1607100

我试过这个:

from R in db.Requests
join RN in db.RequestNumbers on R.RequestNumber equals (RN.Year + RN.Weeknr + (RN.Number.toString().PadLeft(3,char.Parse("0")))

但是无法识别 PadLeft。
还有其他解决方案吗?

[编辑]这是完整的方法:

public List<RequestList> getMyRequests(string userID)
{
var result = (from R in db.Requests
join MT in db.MainTorsList on R.Category equals MT.MainTorsListID into MTL
from MT in MTL.DefaultIfEmpty()
join ST in db.SubTorsList on R.RequestType equals ST.SubTorsListID into STL
from ST in STL.DefaultIfEmpty()
join S in db.StatusList on R.RequestStatus equals S.StatusListID into SL
from S in SL.DefaultIfEmpty()
join RN in db.RequestNumber on R.RequestNumber equals RN.Year + RN.Week + (RN.Number.ToString().PadLeft(3, char.Parse("0"))) into RNL
from RN in RNL.DefaultIfEmpty()
where R.CreatedBy == userID && RN.Removed == false

select new
{
RequestID = R.RequestID,
RequestDate = R.CreatedOn,
RequestNumber = R.RequestNumber,
Category = MT.Name,
RequestType = ST.Name,
Description = R.RequestDescription,
Status = S.Name,
Options = ""
}
);

List<RequestList> requests = (List<RequestList>)result.ToList().ToNonAnonymousList(typeof(RequestList));

return requests;
}

错误信息:

Additional information: LINQ to Entities does not recognize the method 'System.String PadLeft(Int32, Char)' method, and this method cannot be translated into a store expression.

最佳答案

诀窍是使用 DbFunctions.Right像这样

DbFunctions.Right("00" + RN.Number, 3)

即在开头添加足够的零,并从末尾获取所需的确切长度。

LINQ to Entities 支持所有使用的方法(至少在最新的 EF6.1.3 中)。

关于c# - LinQ 如何将 1 之类的整数更改为字符串 001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443857/

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