Lower

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

    Lower

    Hello,

    I am comparing two strings in a LINQ query as follows:

    where t.Name.StartsWi th(q)

    I want to select the record even if t.Name is "New York" and q is
    "new" ... I don't want case to affect it.

    I changed it to:

    where t.Name.StartsWi th(q.ToLower)

    But I don't know how to do this to t.Name ...

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

    #2
    Re: Lower

    shapper wrote:
    I am comparing two strings in a LINQ query as follows:
    >
    where t.Name.StartsWi th(q)
    >
    I want to select the record even if t.Name is "New York" and q is
    "new" ... I don't want case to affect it.
    >
    I changed it to:
    >
    where t.Name.StartsWi th(q.ToLower)
    >
    But I don't know how to do this to t.Name ...
    If t.Name is a String I would assume you could use
    ToLower there as well.

    Arne

    Comment

    • Pavel Minaev

      #3
      Re: Lower

      On Jul 13, 5:49 pm, shapper <mdmo...@gmail. comwrote:
      Hello,
      >
      I am comparing two strings in a LINQ query as follows:
      >
      where t.Name.StartsWi th(q)
      >
      I want to select the record even if t.Name is "New York" and q is
      "new" ... I don't want case to affect it.
      >
      I changed it to:
      >
      where t.Name.StartsWi th(q.ToLower)
      >
      But I don't know how to do this to t.Name ...
      Never, ever, use ToLower/ToUpper for case-insensitive comparison. It
      just doesn't work for some languages. Instead, use the appropriate
      overload:

      t.Name.StartsWi th(q, StringCompariso n.CurrentCultur eIgnoreCase)

      Note the "CurrentCulture IgnoreCase" bit - this is the same behavior as
      StartsWith with only 1 argument, but also ignores case. If you do
      _not_ want to use culture-based comparison (e.g. if you're comparing
      identifiers rather than user-produced text), you might want to
      consider InvariantCultur eIgnoreCase, or even OrdinalIgnoreCa se.

      Comment

      • shapper

        #4
        Re: Lower

        On Jul 13, 8:16 pm, Pavel Minaev <int...@gmail.c omwrote:
        On Jul 13, 5:49 pm, shapper <mdmo...@gmail. comwrote:
        >
        Hello,
        >
        I am comparing two strings in a LINQ query as follows:
        >
        where t.Name.StartsWi th(q)
        >
        I want to select the record even if t.Name is "New York" and q is
        "new" ... I don't want case to affect it.
        >
        I changed it to:
        >
        where t.Name.StartsWi th(q.ToLower)
        >
        But I don't know how to do this to t.Name ...
        >
        Never, ever, use ToLower/ToUpper for case-insensitive comparison. It
        just doesn't work for some languages. Instead, use the appropriate
        overload:
        >
        t.Name.StartsWi th(q, StringCompariso n.CurrentCultur eIgnoreCase)
        >
        Note the "CurrentCulture IgnoreCase" bit - this is the same behavior as
        StartsWith with only 1 argument, but also ignores case. If you do
        _not_ want to use culture-based comparison (e.g. if you're comparing
        identifiers rather than user-produced text), you might want to
        consider InvariantCultur eIgnoreCase, or even OrdinalIgnoreCa se.
        Hi,

        I am just a little bit confused between:
        InvariantCultur eIgnoreCase and CurrentCultureI gnoreCase

        In this case I am using this in an AutoComplete.
        So when a user writes "Ne", which is the value of q, then my Linq will
        look for all records where Name starts with "Ne", "ne", "nE", ...

        Which one should I use in this case? InvariantCultur eIgnoreCase or
        CurrentCultureI gnoreCase?

        Thanks,
        Miguel

        Comment

        • Pavel Minaev

          #5
          Re: Lower

          On Jul 14, 12:55 am, shapper <mdmo...@gmail. comwrote:
          I am just a little bit confused between:
          InvariantCultur eIgnoreCase and CurrentCultureI gnoreCase
          >
          In this case I am using this in an AutoComplete.
          So when a user writes "Ne", which is the value of q, then my Linq will
          look for all records where Name starts with "Ne", "ne", "nE", ...
          >
          Which one should I use in this case? InvariantCultur eIgnoreCase or
          CurrentCultureI gnoreCase?
          For people's names, CurrentCulture is definitely the best, since it's
          comparison of strings in natural language, and it is directly visible
          to the end user.

          Comment

          • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

            #6
            RE: Lower

            Hey there, the comments about using a
            StringCompariso n.CurrentCultur eIgnoreCase are all correct. But if you
            actually are going to to user ToLower on 2 strings to compare them, I
            remember thinking that it is better in .NET to compare two uppercase strings.
            Apparently there is some optimization of the framework for that. I think its
            mentioned in "CLR via C#" (which is the second version of "Applied .NET
            Framework Programming".


            --
            Ciaran O''Donnell
            try{ Life(); } catch (TooDifficultException) { throw Toys(); }



            "shapper" wrote:
            Hello,
            >
            I am comparing two strings in a LINQ query as follows:
            >
            where t.Name.StartsWi th(q)
            >
            I want to select the record even if t.Name is "New York" and q is
            "new" ... I don't want case to affect it.
            >
            I changed it to:
            >
            where t.Name.StartsWi th(q.ToLower)
            >
            But I don't know how to do this to t.Name ...
            >
            Thank You,
            Miguel
            >

            Comment

            • Pavel Minaev

              #7
              Re: Lower

              On Jul 13, 11:16 pm, Pavel Minaev <int...@gmail.c omwrote:
              Never, ever, use ToLower/ToUpper for case-insensitive comparison. It
              just doesn't work for some languages. Instead, use the appropriate
              overload:
              I thought this was a strong enough statement that it warrants some
              clarification, so here are the references:




              And in general, I'd heartily recommend reading a couple of posts
              Michael's blog to anyone who deals with Unicode (which is pretty much
              every programmer out there, these days) to better understand the
              issues involved.

              Comment

              Working...