Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ to SQL - String.IndexOf() does not work with parameters of length 1

LINQ to SQL - String.IndexOf() does not work with parameters of length 1

Scheduled Pinned Locked Moved LINQ
csharpdatabasesql-servervisual-studiolinq
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Penner
    wrote on last edited by
    #1

    Hi, Can anyone explain why LINQ to SQL translates the String.IndexOf(string value) method into this SQL:

    CASE
    WHEN (DATALENGTH(@p0) / 2) = 0 THEN 0
    ELSE CHARINDEX(@p0, [t0].[Name]) - 1
    END

    The WHEN statment restricts parameters to be a minimum length of 2. This prevents me from passing in a string with a length of 1 to the String.IndexOf() method. Is this due to some sort of SQL limitation? Is there away around this? I am running .Net 3.5 with SP1, Visual Studio 2008 with SP1, and SQL Server 2005. Thanks for any help, kp

    J 1 Reply Last reply
    0
    • K Kris Penner

      Hi, Can anyone explain why LINQ to SQL translates the String.IndexOf(string value) method into this SQL:

      CASE
      WHEN (DATALENGTH(@p0) / 2) = 0 THEN 0
      ELSE CHARINDEX(@p0, [t0].[Name]) - 1
      END

      The WHEN statment restricts parameters to be a minimum length of 2. This prevents me from passing in a string with a length of 1 to the String.IndexOf() method. Is this due to some sort of SQL limitation? Is there away around this? I am running .Net 3.5 with SP1, Visual Studio 2008 with SP1, and SQL Server 2005. Thanks for any help, kp

      J Offline
      J Offline
      Jon Rista
      wrote on last edited by
      #2

      Actually, its not a minimum length of 2, its a minimum length of 0. DATALENGTH() returns the byte length, not the character length. L2S usually passes strings in as unicode, so characters are 2 bytes in length. The WHEN check is actually checking for a string length of 0 because any byte length less than 2, in integer divide, will result in 0. Since nvarchar results in lengths that are always multiples of 2, if the string length is 0, then the string your looking for can not be found. CHARINDEX returns 0 when the expression seeked is not found, and 1 or greater if it is found, so this case statement should work for any string length...0 up to nvarchar(max).

      K 1 Reply Last reply
      0
      • J Jon Rista

        Actually, its not a minimum length of 2, its a minimum length of 0. DATALENGTH() returns the byte length, not the character length. L2S usually passes strings in as unicode, so characters are 2 bytes in length. The WHEN check is actually checking for a string length of 0 because any byte length less than 2, in integer divide, will result in 0. Since nvarchar results in lengths that are always multiples of 2, if the string length is 0, then the string your looking for can not be found. CHARINDEX returns 0 when the expression seeked is not found, and 1 or greater if it is found, so this case statement should work for any string length...0 up to nvarchar(max).

        K Offline
        K Offline
        Kris Penner
        wrote on last edited by
        #3

        Okay, that makes sense... and what stuffed me up more is that I was testing it in SQL with 'i' instead of N'i' (using ascii instead of unicode). Well, thanks very much for clearing that up :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups