How to convert a numeric value to string in Linq query

Sometimes there is a need to convert a numeric value to string but the method ‘toString()’ or ‘Convert.ToString ()’ does not work. It is because linq to entities does not support and not aware of these functions.

The workaround in EF V4.0 is to use SqlFunctions.StringConvert

var query= Entities.Users.Where(u => == 'admin').Select(n => new
                                   someString = SqlFunctions.StringConvert((double)n.intValue).Trim()

Just remember to import the reference:

using System.Data.Entity.SqlServer;

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *