1

So, in my last post I was asking how to build a dynamic search filter using LINQ and EF4 (See Here) and finally came up with the solution of building the expression as a string and parse it to an expression using the Dynamic LINQ library.

I that solved the problem. I was able to generate a Expression<Func<TSource, out bool>> and pass it to the Where() method of the DbSet. I am also trying to do this using MySql as a database behind EF4.

The problem came when I tried to apply string operations to integers, like searching a database record which consecutive number starts with 1234.

My initial expression was something like: record.ConsecutiveNumber.ToString().StartsWith("1234"). Sadly, as expected, things were not that easy as EF4 fails to query the DbSet with exception:

"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."

After some Google search I found that this is a common problem. But C'MON! Is there a way to perform a search function that can search records with a consecutive number starting by "1234"?

How pros implement search features with EF4? This is with a single property filter. What if I wanna add multiple filters? God, my head hurts... :/

Thanks!

EDIT:

Thought #1: What about a stored procedure? What about calling a MySql stored procedure from Linq? Am I aiming way too high?

Community
  • 1
  • 1
Luis Aguilar
  • 4,331
  • 6
  • 36
  • 55

5 Answers5

5

You can use the SqlFunctions.StringConvert method. It requires a double (or decimal) so you'll have to cast your int ConsecutiveNumber.

Replace:

record.ConsecutiveNumber.ToString().StartsWith("1234")

With:

SqlFunctions.StringConvert((double)record.ConsecutiveNumber).StartsWith("1234")
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
0

Have you looked at the Dynamic LinQ Library: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

And for your question How to use "contains" or "like" in a dynamic linq query?

Previously I have gotten the code for this lib and just taken a look inside, it is pretty easy to follow.

Community
  • 1
  • 1
Slappy
  • 4,042
  • 2
  • 29
  • 41
  • I've tried that library. In the end it generates a Linq lambda expression, when passed to the where method, the app crashes with an exception stating that ToString is not supported in Linq2Entities. I can make contains or like evaluations on String data but what I want is to evaluate if there are any records whose consecutive number starts with some number. Consecutive number is not a string, is an integer, so my first approach was to convert it to string first and then use contains. But no ToString supported. – Luis Aguilar Mar 30 '11 at 14:50
0

This would be my thought process on getting it to work. Hopefully it points you in the right direction.

According to other posts SqlFunctions.StringConvert((double)record.ConsecutiveNumber) works for Sql Server.

Problem with converting int to string in Linq to entities

And here is relevant information on linq conversions.

Linq int to string

And here is an answer hinting at writing your own sql function for stringconvert

Using a SQL Function in Entity Framework Select

If SqlFunctions.StringConvert doesn't work for you I'd suggest looking at figuring out how to do it in Sql and then writing your own [EdmFunction()] attribute based method.

Community
  • 1
  • 1
JTew
  • 3,149
  • 3
  • 31
  • 39
0

I haven't got a clue if this will work over Linq to EF or not but presuming that they mapped the Math operations, this might solve your need:

record.ConsecutiveNumber / Math.Pow(10, Math.Truncate(Math.Log10(record.ConsecutiveNumber) - 3)) == 1234

This is basically dividing the number by a power of 10 just big enough to leave the first 4 digits.

I know this is very hacky and inefficient even if it works, but there you go. :)

paracycle
  • 7,665
  • 1
  • 30
  • 34
0
Any method calls in a LINQ to Entities query that are not explicitly mapped to a canonical function will result in a runtime NotSupportedException exception being thrown. 

Check mapping canonical function here: http://msdn.microsoft.com/en-us/library/bb738681.aspx

In this case, you can use Math function. (I don't think code first can use in product project at that time)

langtu
  • 1,198
  • 1
  • 10
  • 23