Question

Hello I have a MySQL and LINQ to SQL task to convert Int value from DB to String, toString() is not supported by MySql, and I am getting error about it.

var data = (from o in ObjectContext.MyTable.Where(d => d.a == a)
                              select new MyObject
                              {
                                  Id = o.Id,
                                  StringProperty = o.intColumn.ToString()
                              });

SqlFunction class is not suported for MySql.

Was it helpful?

Solution

You can try to convert the results to Enumerable and then query on it:

var data = (from o in ObjectContext.MyTable.AsEnumerable().Where(d => d.a == a)
                              select new MyObject
                              {
                                  Id = o.Id,
                                  StringProperty = o.intColumn.ToString()
                              });

OTHER TIPS

You could fetch all the data by adding AsEnumerable() to the table, or you could do the fetch first, then the conversion later - if I'm not mistaken, this should produce a slightly lighter SQL call:

var tempData = (from o in ObjectContext.Where(d => d.a == a) select o);

var converted = (from o in tempData.AsEnumerable()
                select new MyObject
                          {
                              Id = o.Id,
                              StringProperty = o.intColumn.ToString()
                          });

Alternatively, write it all in one go by using the first part as a sub query:

var data = from x in 
                  (from o in ObjectContext.Where(d => d.a == a) select o)
                  .AsEnumerable()
           select new MyObject
                           {
                              Id = x.Id,
                              StringProperty = x.intColumn.ToString()
                          });

int to string in Entity Framework also had supported a great solution you can refer. By this method you can change you source as the following.

var data = (from o in ObjectContext.MyTable.Where(d => d.a == a)
                          select new MyObject
                          {
                              Id = o.Id,
                              StringProperty = SqlFunctions.StringConvert((double)o.intColumn)
                          });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top