How to use ToString () method to convert integer to string inside LINQ

When I try to use ToString () inside a LINQ Lambda expression, I get an exception: "LINQ to Entities does not recognize the System.String ToString () method, and this method cannot be translated into a stored expression."

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

I use Linq for entites. And the database uses MySQL, not SQL Server. Immediate help would be greatly appreciated.

+3
source share
3 answers

I solved this problem by directly writing a MySQl request inside C #, as shown below -

string queryTemplate = 
 @"select inv.* from invoices as inv where userID = '123' and date like '%abc%'";
List<invoice> totalSearch = 
 context.ExecuteStoreQuery<invoice>(queryTemplate).ToList();
+5
source

Harshal, SqlFunctions.StringConvert MS SQL, MySQL. Enumerable . :

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

:

l db.Customers.AsEnumerable()

Enumerable, .toString() . .

+4

Source: https://habr.com/ru/post/1776856/


All Articles