1
votes

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.

1
What database do you use? What is the exact error if you use SqlFunctions.PatIndex and how do you use it?CodeCaster
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

1 Answers

-1
votes

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