ASP.NET MVC Search Page - Integer StartsWith On Linq + EF4

后端 未结 5 1704
谎友^
谎友^ 2021-01-03 16:58

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 str

相关标签:
5条回答
  • 2021-01-03 17:09
    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)

    0 讨论(0)
  • 2021-01-03 17:20

    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.

    0 讨论(0)
  • 2021-01-03 17:24

    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.

    0 讨论(0)
  • 2021-01-03 17:24

    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")
    
    0 讨论(0)
  • 2021-01-03 17:26

    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. :)

    0 讨论(0)
提交回复
热议问题