How to convert an integer to a string using the ToString() function in LINQ

c# entity entity-framework linq mysql


I get an error stating that "LINQ to Entities does not recognize the method 'System.String ToString()' function, and this method cannot be translated into a store expression" when I attempt to use ToString() within the following LINQ Lambda expression.

query = query.Where(q =>
                          || q.invoicenum.ToString().Contains(filtertext)
                          || q.trans_type.ToString().Contains(filtertext)
                          || q.charge.Contains(filtertext));

I'm making use of Linq to entities. And MySQL, not SQL Server, is the database in use. Help right now would be much appreciated.

11/25/2010 9:52:39 AM

Accepted Answer

I fixed this problem by explicitly putting the following MySQl query inside of C#:

string queryTemplate = 
 @"select inv.* from invoices as inv where userID = '123' and date like '%abc%'";
List<invoice> totalSearch = 
11/27/2010 5:13:26 AM

Popular Answer

Harshal, the SqlFunctions are the problem. Instead of MySQL, StringConvert is for MS SQL. You may try converting the output to an Enumerable and then running a query on it. Example:

using (DatabaseEntities db = new DatabaseEntities())
            var list = from l in db.Customers.AsEnumerable()
                       orderby l.CompanyName
                       select new SelectListItem { Value = l.CustomerID.ToString(), Text = l.CompanyName };

            return list.ToList();

Observe the line:

l in db.Customers.AsEnumerable()

You may use.toString() on it after is turning the outcomes to Enumerable. This may be changed to suit your requirements.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow