Compare Strings in Linq. Need advice ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Compare Strings in Linq. Need advice ...

    Hello,

    I want to compare two strings in a Linq Query.

    In this case "Car", "cAr", "CAR" would all be the same.

    Should I use ==, equals, ... ?

    What is the best way to do this?

    Thanks,
    Miguel
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Compare Strings in Linq. Need advice ...

    shapper wrote:
    I want to compare two strings in a Linq Query.
    >
    In this case "Car", "cAr", "CAR" would all be the same.
    >
    Should I use ==, equals, ... ?
    >
    What is the best way to do this?
    One of the String.Compare' s.

    Arne

    Comment

    • Peter Duniho

      #3
      Re: Compare Strings in Linq. Need advice ...

      On Thu, 21 Aug 2008 18:47:19 -0700, shapper <mdmoura@gmail. comwrote:
      Hello,
      >
      I want to compare two strings in a Linq Query.
      >
      In this case "Car", "cAr", "CAR" would all be the same.
      >
      Should I use ==, equals, ... ?
      For the String class, == or Equals() should be equivalent. Of course,
      you'll have to convert the strings to all lower or all upper case or
      (preferably, IMHO) use one of the String.Compare( ) overloads that allows
      you to tell it to ignore case (in that case, you'd use neither == or the
      Equals() method). Take care to choose your culture-dependent or
      culture-invariant case-handling as appropriate to your needs.

      Pete

      Comment

      • Marc Gravell

        #4
        Re: Compare Strings in Linq. Need advice ...

        What is the best way to do this?

        It depends *which* LINQ back-end. If you mean LINQ-to-SQL against a
        case-insensitive database, it won't matter. If you mean LINQ-to-
        Objects, then one of the overloads that accepts the comparison type
        (such as case-insensitive) would be preferable. However, for LINQ-to-
        SQL against a case-sensitive database, I think .ToLower / .ToUpper
        might be your necessity, an you'll have to just avoid the Turkish i
        problem. For index use, you might want to store a case-normalized
        version of the value so you can hit that...

        Marc

        Comment

        • shapper

          #5
          Re: Compare Strings in Linq. Need advice ...

          On Aug 22, 5:03 am, Marc Gravell <marc.grav...@g mail.comwrote:
          What is the best way to do this?
          >
          It depends *which* LINQ back-end. If you mean LINQ-to-SQL against a
          case-insensitive database, it won't matter. If you mean LINQ-to-
          Objects, then one of the overloads that accepts the comparison type
          (such as case-insensitive) would be preferable. However, for LINQ-to-
          SQL against a case-sensitive database, I think .ToLower / .ToUpper
          might be your necessity, an you'll have to just avoid the Turkish i
          problem. For index use, you might want to store a case-normalized
          version of the value so you can hit that...
          >
          Marc

          I am using SQL Server 2005 so I think culture does not matter here ...
          in fact if I use it I get an error.

          I am using the following:

          bool check = (from t in database.Tags
          where t.Name == Name
          select t).Any();

          How would I use String.Compare in this? I tried but I get an error:
          Cannot implicitly convert type 'int' to 'bool'

          bool check = (from t in database.Tags
          where t.Name.CompareT o(Name)
          select t).Any();

          Basically I am only trying the best way to check if a tag with given
          Name exists in database Tags.

          Thanks,
          Miguel

          Comment

          • Marc Gravell

            #6
            Re: Compare Strings in Linq. Need advice ...

            Why do you want to use CompareTo? It sounds like you are doing an
            equality test, which is what you already have...?

            Marc

            Comment

            • Peter Duniho

              #7
              Re: Compare Strings in Linq. Need advice ...

              On Fri, 22 Aug 2008 04:16:09 -0700, shapper <mdmoura@gmail. comwrote:
              [...]
              How would I use String.Compare in this? I tried but I get an error:
              Cannot implicitly convert type 'int' to 'bool'
              That's because the CompareTo() method returns an int, not a bool.

              If you're using the CompareTo() method, then I agree with Marc that you
              should just use the == operator.

              But your original question indicated you wanted a case-insensitive
              comparison, in which case you'd use one of the String.Compare( ) overloads,
              not String.CompareT o().

              Resolving both mistakes at once, you could have:

              bool check = (from t in database.Tags
              where String.Compare( t.Name, Name,
              StringCompariso n.InvariantCult ureIgnoreCase)
              == 0
              select t).Any();

              Note the comparison of the result of Compare() to the value 0, which is
              what it returns when the two strings are equal.

              This also assumes, of course, that you want the invariant culture. There
              are other comparison options that ignore case for other types of
              comparisons.

              Pete

              Comment

              Working...