Reputation: 58873
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
Upvotes: 47
Views: 57972
Reputation: 36438
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
Upvotes: 32
Reputation: 11
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.
Upvotes: -1
Reputation: 8552
from s in ctx.Services.ToList()
where s.Code.ToString().StartsWith("1")
select s
Upvotes: -1
Reputation: 11
I tried this workaround and it worked for me:
from s in ctx.Services
where (s.Code + "").StartsWith("1")
select s
Hope this helps
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 5534
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.
Upvotes: 54
Reputation: 21
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
});
Upvotes: 2
Reputation: 17683
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 );
Upvotes: -1