.net Linq int 到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:06:54  来源:igfitidea点击:

Linq int to string

.netlinqlinq-to-entities

提问by pistacchio

how do I cast and int into a string? None of the following do works:

我如何将和 int 转换为字符串?以下方法均无效:

from s in ctx.Services
    where s.Code.ToString().StartsWith("1")
    select s

from s in ctx.Services
    where Convert.ToString(s.Code).StartsWith("1")
    select s

from s in ctx.Services
    where ((string)s.Code).ToString().StartsWith("1")
    select s

EDIT

编辑

The error I get is:

我得到的错误是:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

采纳答案by ShuggyCoUk

Linq to Entities does not support as many conversion of functions to server hosted sql.

Linq to Entities 不支持将函数转换为服务器托管的 sql 的次数。

ToString (on anything) is one of them

ToString(在任何东西上)就是其中之一

Here's the list of supported functions

这是支持的功能列表

This has been asked before on Stack overflow specifically asking how to convert an int to a string

之前在 Stack Overflow 上已经问过这个问题,专门询问如何将 int 转换为字符串

回答by Brian Cauthon

With EF v4 you can use SqlFunctions.StringConvert. So your code would end looking like this:

使用 EF v4,您可以使用SqlFunctions.StringConvert。所以你的代码会像这样结束:

from s in ctx.Services
where SqlFunctions.StringConvert((double)s.Code).StartsWith("1")
select s

EDIT

编辑

As Rick mentions in his comment, the reason for casting the int to a double (decimal probably works too) is that the function does not have an overload for int.

正如 Rick 在他的评论中提到的,将 int 转换为 double(十进制也可能有效)的原因是该函数没有 int 的重载。

回答by jboy

I had a similar issue

我有一个类似的问题

        IQueryable<Street> query = db.Streets.AsQueryable();
        query = query.Where(street => street.Name.ToLower().StartsWith(keyword))
                        .Take(10)
                        .OrderBy(street => street.Name);

        var list = query.Select(street => new
        {
            street.StreetId,
            Name = street.Name + " " + street.Suburb.Name + " " + street.Suburb.State.Name + " " + street.Suburb.Postcode

        });

street.Suburb.Postcode was throwing 'cannot convert non primative type error' as its an int? After following shuggy's link I fixed by adding .AsEnumerable to force int? -> string conversion 'client side' i.e. LINQ to Objects. i.e.

street.Suburb.Postcode 抛出“无法转换非原始类型错误”作为它的一个整数?在关注 shuggy 的链接后,我通过添加 .AsEnumerable 来强制 int 进行修复?-> 字符串转换“客户端”,即 LINQ to Objects。IE

var list = query.AsEnumerable().Select(street => new
        {
            street.StreetId,
            Name = street.Name + " " + street.Suburb.Name + " " + street.Suburb.State.Name + " " + street.Suburb.Postcode

        });

回答by Mike

When using LINQ to Objects you can use the ToString() helper.

使用 LINQ to Objects 时,您可以使用 ToString() 助手。

回答by neuralsea

cast IQueryable to IEnumerable since IQueryable inherits IEnumerable. see http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx- specifically: 'Enumeration causes the expression tree associated with an IQueryable object to be executed.' I think the important thing is 'The definition of "executing an expression tree" is specific to a query provider'.

将 IQueryable 转换为 IEnumerable,因为 IQueryable 继承了 IEnumerable。请参阅http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx- 特别是:“枚举导致与 IQueryable 对象关联的表达式树被执行。” 我认为重要的是“执行表达式树”的定义特定于查询提供程序。

IQueryable is very powerful but misused in this context.

IQueryable 非常强大,但在这种情况下被误用。

回答by Everto

I tried this workaround and it worked for me:

我尝试了这个解决方法,它对我有用:

from s in ctx.Services
where (s.Code + "").StartsWith("1")
select s

Hope this helps

希望这可以帮助

回答by Thulasiram

from s in ctx.Services.ToList()
    where s.Code.ToString().StartsWith("1")
    select s   

回答by Ken Blackford

var items = from c in contacts
select new ListItem
{
    Value = String.Concat(c.ContactId), //This Works in Linq to Entities!
    Text = c.Name
};

I found that SqlFunctions.StringConvert((double)c.Age) did not work for me either the field is of type Nullable

我发现 SqlFunctions.StringConvert((double)c.Age) 对我不起作用,或者该字段的类型为 Nullable

Took me a lot of searching over the last few days of trial and error to find this.

在过去几天的反复试验中,我花了很多时间搜索才找到这个。

I hope this helps a few coders out there.

我希望这对那里的一些编码人员有所帮助。

回答by rp.

Here is a way to do it.

这是一种方法。

int[] x = new int[] {11,3,15,7,19};

var j = from s in x where s.ToString().StartsWith( "1") select s.ToString();

Console.WriteLine( j );