By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,894 Members | 1,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,894 IT Pros & Developers. It's quick & easy.

Lower

P: n/a
Hello,

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

where t.Name.StartsWith(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.StartsWith(q.ToLower)

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

Thank You,
Miguel
Jul 13 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
shapper wrote:
I am comparing two strings in a LINQ query as follows:

where t.Name.StartsWith(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.StartsWith(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
Jul 13 '08 #2

P: n/a
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.StartsWith(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.StartsWith(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.StartsWith(q, StringComparison.CurrentCultureIgnoreCase)

Note the "CurrentCultureIgnoreCase" 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 InvariantCultureIgnoreCase, or even OrdinalIgnoreCase.
Jul 13 '08 #3

P: n/a
On Jul 13, 8:16*pm, Pavel Minaev <int...@gmail.comwrote:
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.StartsWith(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.StartsWith(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.StartsWith(q, StringComparison.CurrentCultureIgnoreCase)

Note the "CurrentCultureIgnoreCase" 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 InvariantCultureIgnoreCase, or even OrdinalIgnoreCase.
Hi,

I am just a little bit confused between:
InvariantCultureIgnoreCase and CurrentCultureIgnoreCase

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? InvariantCultureIgnoreCase or
CurrentCultureIgnoreCase?

Thanks,
Miguel
Jul 13 '08 #4

P: n/a
On Jul 14, 12:55*am, shapper <mdmo...@gmail.comwrote:
I am just a little bit confused between:
InvariantCultureIgnoreCase and CurrentCultureIgnoreCase

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? InvariantCultureIgnoreCase or
CurrentCultureIgnoreCase?
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.
Jul 14 '08 #5

P: n/a
Hey there, the comments about using a
StringComparison.CurrentCultureIgnoreCase 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
http://wannabedeveloper.spaces.live.com
"shapper" wrote:
Hello,

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

where t.Name.StartsWith(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.StartsWith(q.ToLower)

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

Thank You,
Miguel
Jul 14 '08 #6

P: n/a
On Jul 13, 11:16*pm, Pavel Minaev <int...@gmail.comwrote:
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:

http://blogs.msdn.com/michkap/archiv...15/551773.aspx
http://blogs.msdn.com/michkap/archiv...11/511557.aspx

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.
Jul 15 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.