Linq int to string

∥☆過路亽.° 提交于 2019-12-27 12:02:12

问题


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

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


回答1:


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

ToString (on anything) is one of them

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




回答2:


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

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.




回答3:


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.

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

        });



回答4:


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




回答5:


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 is very powerful but misused in this context.




回答6:


I tried this workaround and it worked for me:

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

Hope this helps




回答7:


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 );



回答8:


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



回答9:


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

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.



来源:https://stackoverflow.com/questions/1228318/linq-int-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!