32

How do you convert an int to a string in Link to EF?

The clr cant imagine casting an int to a string and Entity framework cant figure out what SQL snippet to translate .ToString() into.

So how do you write a linq statement that returns a string instead of an int?

3
  • 2
    What version of EF are you using? Commented Dec 21, 2010 at 19:00
  • EF version 4. Does that make a difference? Commented Dec 23, 2010 at 4:07
  • 1
    EF >= 6.1 supports ToString: blogs.msdn.com/b/adonet/archive/2014/03/17/… Commented Aug 21, 2014 at 7:03

1 Answer 1

54

Sadly EF does not know how to convert .ToString(). You must use the embedded function SqlFunctions.StringConvert: http://msdn.microsoft.com/en-us/library/dd466292.aspx Also there is no overload for int so you must typecast to double :-(

var vendors = 
   from v in Vendors  
   select new
   {             
       Code = SqlFunctions.StringConvert((double)v.VendorId)
   }; 
Sign up to request clarification or add additional context in comments.

4 Comments

B"H Wow, that sucks. What I do now is always project to an intermidiate (usualy anonymous) type in the select and then project into my real objects in the next step after a AsEnumerable() or something like that
i have to pass this converted value in where clause
It's also often important to call Trim() on the output to avoid having your string come out padded.
And how does one use this in EntityDataSource?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.