Linq int to string
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.
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
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
});
When using LINQ to Objects you can use the ToString() helper.
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.