0

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.

1
  • ToString() (capital T) Commented Feb 27, 2014 at 10:21

3 Answers 3

1

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

var data = (from o in ObjectContext.MyTable.Where(d => d.a == a)
                              select new MyObject
                              {
                                  Id = o.Id,
                                  StringProperty = o.intColumn.ToString()
                              }).AsEnumerable();
Sign up to request clarification or add additional context in comments.

2 Comments

This will work, but all the data in the table will be enumerated (fetched) here, if I'm not mistaken. May be a bad idea if you have lots of data.
At least do .AsEnumerable() after the Where.
0

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()
                          });

Comments

0

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)
                          });

Comments

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.