3

When I try to use ToString() inside the below LINQ Lambda expression, I get an exception saying "LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store 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 am using Linq to entites. And the Database used is MySQL and not the SQL Server. Immediate help would be highly appreciated.

Itay Karo
  • 17,924
  • 4
  • 40
  • 58
CHash11
  • 746
  • 4
  • 14
  • 31
  • 3
    For immediate help it is often useful to search SO first: http://stackoverflow.com/questions/1228318/linq-int-to-string ,http://stackoverflow.com/questions/1066760/problem-with-converting-int-to-string-in-linq-to-entities – Macros Nov 25 '10 at 09:55
  • Try concatenating with an empty string. – Klaus Byskov Pedersen Nov 25 '10 at 09:55
  • check the query.ToString() to see what is the sql query being generated. you will figure out why the ToString() is causing problems – Ali Tarhini Nov 25 '10 at 09:56
  • Thanks guys for ur reply.. But please note above work arounds do not work for me as I am using MySQL as a database and not SQL Server. Hence i looged this on separate thread... – CHash11 Nov 25 '10 at 10:28

3 Answers3

5

I resolved this issue by directly writing MySQl query inside C# as below -

string queryTemplate = 
 @"select inv.* from invoices as inv where userID = '123' and date like '%abc%'";
List<invoice> totalSearch = 
 context.ExecuteStoreQuery<invoice>(queryTemplate).ToList();
TJB
  • 13,367
  • 4
  • 34
  • 46
CHash11
  • 746
  • 4
  • 14
  • 31
4

Harshal, the issue is the SqlFunctions.StringConvert is for MS SQL, not for MySQL. You can try convert the results to Enumerable and then 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();
        }

Note the line:

l in db.Customers.AsEnumerable()

is converting the results to Enumerable, then you can use .toString() on it. You can adapt this to your needs.

Nestor
  • 1,969
  • 4
  • 25
  • 30
1

In EF 4 you can use SqlFunctions.StringConvert

Stefan P.
  • 9,489
  • 6
  • 29
  • 43
  • Does EF4 comes by default with .Net Framework 4? I have .Net Framework 4, but still i get below error "The specified method 'System.String StringConvert(System.Nullable`1[System.Double])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression." Please note i am using MySQL as database and not SQL Server.. – CHash11 Nov 25 '10 at 10:25
  • EF4 can connect to MySQL see this article: http://dev.mysql.com/doc/refman/5.4/en/connector-net-tutorials-entity-framework-winform-data-source.html – Stefan P. Nov 25 '10 at 10:33
  • Ok.. But does EF4 comes with .Net framework 4.0? How can i check which is the entity framework used in my app... I am using .Net Framework 4.0, if it comes with EF4 then i should not get above error. – CHash11 Nov 25 '10 at 10:37
  • Try this: query = query.Where(q => SqlFunctions.StringConvert((double)q.invoicenum).Contains(filtertext)); let me know if it works for you. – Stefan P. Nov 25 '10 at 10:49
  • Hi Stefan, I get this error with this code - "The specified method 'System.String StringConvert(System.Nullable`1[System.Double])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression." – CHash11 Nov 25 '10 at 11:07
  • ok, seems that you have to map your database with EF4, EF4 is shipped with .NET 4 and VS.NET 2010, just follow the article I posted earlier from dev.mysql.com – Stefan P. Nov 25 '10 at 11:10
  • But we are using .Net 4 and VS.Net 2010. All other EF queries are working fine.. Except this one. – CHash11 Nov 25 '10 at 11:16
  • Hi Stefan, Do you know how can I map my SQl database to EF4 separately for this query to work. Becasue we followed the same steps mentioned in the dev.mysql.com to setup the EntityFramework and we are using the .Net 4 from the begining, so i dont think we have missed any steps as metioned in the dev.mysql.com – CHash11 Nov 25 '10 at 11:28
  • Very strange, I just tested this on SQL Server 2008 and it works... and it should not matter the database server, the SqlFunctions should be database independent. Maybe you could post this question on mysql forum and see if they got the same problem. – Stefan P. Nov 25 '10 at 11:39
  • ok..sure...thanks Stefan... Just one question, will the SSDLToMySQL.tt (VS) settings on edmx file properties play some role here? Its set to "SSDLToSQL10.tt (VS)" in my application. – CHash11 Nov 25 '10 at 11:58
  • 1
    I resolved this issue by directly writing MySQl query inside C# as below - string queryTemplate = @"select inv.* from invoices as inv where userID = '123' and date like '%abc%'"; List totalSearch = context.ExecuteStoreQuery(queryTemplate).ToList(); – CHash11 Nov 27 '10 at 05:04
  • 1
    Just for the record, I have similar problem with SqlFunctions.StringConvert(double), in my case I'm using SQL CE4, and I still havent found a solution yet. – Nestor Apr 24 '11 at 15:37