1

I need to remove leading zeros from a string. I'm trying to use TrimStart('0') in a linq to entities query and its not supported. I found this related question, but that solution didn't work for me, it says that function is not supported either. Any suggestions? Thanks!

Update: Using Sql Server and this is how I use it...

Where(c => c.StringToGet.Substring(SqlFunctions.PatIndex("%[^0]%", c.StringToGet).Value - 1) == stringToCompare)

Error... LINQ to Entities does not recognize the method 'System.Nullable`1[System.Int32] PatIndex(System.String, System.String)' method, and this method cannot be translated into a store expression.

Community
  • 1
  • 1
Evan
  • 836
  • 1
  • 13
  • 25
  • What database do you use? What is the exact error if you use `SqlFunctions.PatIndex` and _how_ do you use it? – CodeCaster Jan 07 '14 at 23:08
  • Well if you have to do it client side get them with leading zeros, and then trim them locally. An other option given you are pulling them into a class would be to simply add a trimleadingzeros method to it. – Tony Hopkinson Jan 07 '14 at 23:14

1 Answers1

-1

Do a ToList() on you query first. Then you can you use Trim as it won't be sql operation anymore.

realnero
  • 994
  • 6
  • 12
  • 2
    You don't want this if `TRIM()` is supposed to be used in a `WHERE` clause, as it'll download the entire table and filter on the client. – CodeCaster Jan 08 '14 at 08:30
  • 1
    Yeah, ideally I'd perform this on the database side. – Evan Jan 08 '14 at 23:43