47

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

ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101
pistacchio
  • 56,889
  • 107
  • 278
  • 420
  • The first two ought to work...can you elaborate on what "None of the following...work" means? – lc. Aug 04 '09 at 15:54
  • Are you just not getting any results back? Have you confirmed that the "Code" column contains a value that starts with a "1"? – Keith Aug 04 '09 at 15:55
  • 1
    www.linqpad.net is great for learning Linq (and pretty much anything else in .NET). It's free and highly recommended. Also, its $19 Intellisense is great. – rp. Aug 04 '09 at 16:01
  • 6
    He is using Linq to Entities not Linq to sql - this means much of the advice here is not applicable... – ShuggyCoUk Aug 04 '09 at 16:23

9 Answers9

54

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.

Brian Cauthon
  • 5,524
  • 2
  • 24
  • 26
  • 10
    This worked for me! The only funny thing about this is that apparently there is no overload of this function for 'int'. That's why Brian is boxing this as a double before calling the function above. In my opinion this needs to be marked as the answer for this question. – Rick Arthur Jul 22 '10 at 16:23
  • This, too, worked for me. I agree with Rick Arthur that this should be the answer. – Alban Sep 02 '11 at 13:30
  • 2
    this answer is now more useful than mine, if the op wants to change the accepted answer that would be fine, otherwise I'll add a note to indicate that, from v4 you should use your answer. – ShuggyCoUk Nov 07 '11 at 11:56
  • 6
    +1, but I would add that I found I had to .Trim() before the .StartsWith() because EF was left padding the number with spaces. – Joel Brown Dec 29 '11 at 17:10
  • @JoelBrown - thanks! Saw this approach in a number of places, but I could not figure out why it wasn't working. The Trim() call got it straightened out. – daveaglick Sep 12 '14 at 14:39
32

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

Community
  • 1
  • 1
ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101
2

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

        });
jboy
  • 21
  • 1
2

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

Mike
  • 123
  • 8
1

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.

neuralsea
  • 11
  • 1
  • This was suggested in [this similar question](http://stackoverflow.com/questions/1765423/problem-getting-guid-string-value-in-linq-to-entity-query). The nice thing about this approach is it works for non-numerical data, such as Guids. – Matt Penner Aug 01 '11 at 18:34
1

I tried this workaround and it worked for me:

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

Hope this helps

Everto
  • 11
  • 1
-1
from s in ctx.Services.ToList()
    where s.Code.ToString().StartsWith("1")
    select s   
Thulasiram
  • 8,432
  • 8
  • 46
  • 54
-1

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 );
rp.
  • 17,483
  • 12
  • 63
  • 79
-1
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.

  • 1
    I'm trying to select SelectListItems and casting an int ID into the string "Value" property, same as your example. But with this code I get `LINQ to Entities does not recognize the method 'System.String Concat(System.Object)' method, and this method cannot be translated into a store expression.` – Neil Laslett Aug 23 '13 at 20:12