LinQ如何将像1这样的整数更改为字符串001 [英] LinQ how to change a integer like 1 to string 001

查看:79
本文介绍了LinQ如何将像1这样的整数更改为字符串001的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table in my database with four columns:

string: year
string: weeknr
int:    number

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

In other tables I combine these columns into yywwnnn as a string.

数字列是标识列.

现在,我想从一个表中检索一些要与上述表连接的记录.

Now I want to retrieve some records from a table that I want to join with the mentioned table.

类似:

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

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

But of course RN.Number is a integer and I need it to be a 3 digit string.

如此:
16 07 1 ==> 1607001
16 07 10 ==> 1607010
16 07 100 ==> 1607100

so:
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无法识别.
还有其他解决方案吗?

But PadLeft is not recognized.
Is there any other solution to this?

这是完整的方法:

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.

推荐答案

诀窍是使用

即在开头添加足够的零,并从结尾开始获取确切的长度.

i.e. prepend enough zeros at the beginning and take the exact length needed from the end.

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

All the used methods are supported by LINQ to Entities (at least in the latest at the moment EF6.1.3).

这篇关于LinQ如何将像1这样的整数更改为字符串001的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆