none
Linq is converting the "#" character to "[0-9]" in query RRS feed

  • Question

  • I have the following linq query:

    Parties.Where(Function(party) party.PartyName Like "#" and party.PartyName <> "")

    However when running from my application (or LinqPad) the "#" is being changed to "[0-9]", so the resulting query will look like this:

    -- Region Parameters
    DECLARE @p0 VarChar(1000) = '[0-9]'
    DECLARE @p1 VarChar(1000) = ''
    -- EndRegion
    SELECT [t0].[PartyID], [t0].[DocID], [t0].[PartyName], [t0].[PartyType], [t0].[SortOrder], [t0].[CleanPartyName]
    FROM [Parties] AS [t0]
    WHERE ([t0].[PartyName] LIKE @p0 ESCAPE '~') AND ([t0].[PartyName] <> @p1)

    See how @p0 is being set to '[0-9]'? This has to be some type of Linq feature, but how do I escape it so that I can actually

    search for the "#" character in a query?

    Thanks!

    Thursday, March 3, 2016 10:00 PM

Answers

  • Contains does not replace the "#" character, but it also does not work for searching against strings like this:

    %HIPP,%JOHN%A%LLLP%#1%

     I was pointed to some MSDN documentation which states this:

    "To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), enclose them in brackets."

    That is exactly what I was looking for, I just couldn't find it through google. Enclosing the "#" in brackets works perfectly.

    • Marked as answer by jfk59102 Friday, March 4, 2016 5:03 PM
    Friday, March 4, 2016 5:03 PM

All replies

  • I would change the Like to Contains , but I've never seen what you are showing (then again I don't use Like.  I think the problem could be that Linq is using the VB Like and that is doing the weird translation.

    Lloyd Sheen

    Thursday, March 3, 2016 10:08 PM
  • Contains does not replace the "#" character, but it also does not work for searching against strings like this:

    %HIPP,%JOHN%A%LLLP%#1%

     I was pointed to some MSDN documentation which states this:

    "To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), enclose them in brackets."

    That is exactly what I was looking for, I just couldn't find it through google. Enclosing the "#" in brackets works perfectly.

    • Marked as answer by jfk59102 Friday, March 4, 2016 5:03 PM
    Friday, March 4, 2016 5:03 PM