Linq int to string

how can i draw and int to string? None of the following work:

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

+48
linq linq-to-entities
Aug 04 '09 at 15:50
source share
9 answers

Linq to Entities does not support so many function conversions on the server hosting sql.

ToString (on either) is one of them

Here is a list of supported features.

This was previously asked about stack overflow on how to convert int to string

+33
Aug 04 '09 at 16:22
source share

With EF v4 you can use SqlFunctions.StringConvert . So your code will look 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 including int in double (maybe a decimal number works) is because the function has no overload for int.

+55
Jul 20 '10 at 18:21
source share

I had a similar problem

  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 throws 'can not convert non primative type error' as its int? After the next shuggy link, I fixed it by adding .AsEnumerable to force an int? β†’ conversion of strings "client side", i.e. LINQ to Objects. those.

 var list = query.AsEnumerable().Select(street => new { street.StreetId, Name = street.Name + " " + street.Suburb.Name + " " + street.Suburb.State.Name + " " + street.Suburb.Postcode }); 
+2
Jan 21
source share

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

+2
Apr 02 2018-12-12T00:
source share

distinguishes IQueryable from IEnumerable, because IQueryable inherits IEnumerable. see http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx in particular: "Enumeration forces the expression tree associated with the IQueryable to run." I think the important thing: "The definition of" execution of the expression tree "refers to the query provider."

IQueryable is very powerful, but is used incorrectly in this context.

+1
Mar 25 '11 at 20:20
source share

I tried this workaround and it worked for me:

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

Hope this helps

+1
Mar 22 2018-12-22T00:
source share

Here's how 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 ); 
-one
Aug 4 '09 at 15:59
source share
 from s in ctx.Services.ToList() where s.Code.ToString().StartsWith("1") select s 
-one
May 18 '12 at 2:14 pm
source share
 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) does not work for me, or the field is of type Nullable

Over the past few days of trial and error, I have searched a lot to find this.

I hope this helps a few encoders.

-one
Dec 12 '12 at 10:40
source share



All Articles