PC Review


Reply
Thread Tools Rate Thread

Compare. Case

 
 
shapper
Guest
Posts: n/a
 
      12th Apr 2009
Hello,

I am filtering items from a list as follows:

IQueryable<Account> students = accounts.Where(a => a.Roles.Contains
(givenRole)).AsQueryable();

Roles is a List<String>

How can I check if Roles.Contains givenRole ignoring case?
Which means that if Roles contains "Admin" then any givenRole as
"ADMIN", "Admin", "AdMin" would pass.

Thanks,
Miguel
 
Reply With Quote
 
 
 
 
Mr. Arnold
Guest
Posts: n/a
 
      12th Apr 2009

"shapper" <(E-Mail Removed)> wrote in message
news:d053553e-e527-4999-b08b-(E-Mail Removed)...
> Hello,
>
> I am filtering items from a list as follows:
>
> IQueryable<Account> students = accounts.Where(a => a.Roles.Contains
> (givenRole)).AsQueryable();
>
> Roles is a List<String>
>
> How can I check if Roles.Contains givenRole ignoring case?
> Which means that if Roles contains "Admin" then any givenRole as
> "ADMIN", "Admin", "AdMin" would pass.
>


Most would use ToUpper() on the string to be checked and on the string
doing the checking to make them all the same case so that the check can't be
missed.

 
Reply With Quote
 
Chris Taylor
Guest
Posts: n/a
 
      12th Apr 2009
Hi,

You could use the overload of Contains that takes a StringComparer, for
example
IQueryable<Account> students = accounts.Where(a =>
a.Roles.Contains(givenRole,
StringComparer.InvariantCultureIgnoreCase)).AsQueryable();

Of course you could use a culture specific comparer if that better suites
your requirements, I simple used the Invariant comparer for the example.

Hope this helps

--
Chris Taylor
http://taylorza.blogspot.com
http://dotnetjunkies.com/weblog/chris.taylor



"shapper" <(E-Mail Removed)> wrote in message
news:d053553e-e527-4999-b08b-(E-Mail Removed)...
> Hello,
>
> I am filtering items from a list as follows:
>
> IQueryable<Account> students = accounts.Where(a => a.Roles.Contains
> (givenRole)).AsQueryable();
>
> Roles is a List<String>
>
> How can I check if Roles.Contains givenRole ignoring case?
> Which means that if Roles contains "Admin" then any givenRole as
> "ADMIN", "Admin", "AdMin" would pass.
>
> Thanks,
> Miguel


 
Reply With Quote
 
Pavel Minaev
Guest
Posts: n/a
 
      12th Apr 2009
On Apr 12, 10:01*am, "Mr. Arnold" <MR. Arn...@Arnold.com> wrote:
> "shapper" <mdmo...@gmail.com> wrote in message
>
> news:d053553e-e527-4999-b08b-(E-Mail Removed)...
>
> > Hello,

>
> > I am filtering items from a list as follows:

>
> > IQueryable<Account> students = accounts.Where(a => a.Roles.Contains
> > (givenRole)).AsQueryable();

>
> > Roles is a List<String>

>
> > How can I check if Roles.Contains givenRole ignoring case?
> > Which means that if Roles contains "Admin" then any givenRole as
> > "ADMIN", "Admin", "AdMin" would pass.

>
> Most would use ToUpper() on the string to *be checked *and on the string
> doing the checking to make them all the same case so that the check can'tbe
> missed.


Except that using ToUpper() for case-insensitive comparison is a bad
idea, because it is not a lossless conversion (so strings that would
normally compare as not equal, even with case-insensitive comparison
enabled, may compare equal after ToUpper()). An example of a letter
that may trigger such behavior is "dotless I" in Turkish locale; see
http://en.wikipedia.org/wiki/Dotted_and_dotless_I

The correct way to perform case-insensitive comparisons, as Chris
demonstrated, is to use a comparison object with case-insensitive
semantics.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Sensitive Compare, Lookup, etc???? =?Utf-8?B?R1NrZXJt?= Microsoft Excel Misc 5 23rd Oct 2007 04:08 AM
Compare Worksheet Names - Ignore Case? =?Utf-8?B?VHJhZGVyX2luX1BhcmFkaXNl?= Microsoft Excel Programming 3 12th May 2007 12:31 AM
Compare Upper and Lower Case Zone Microsoft Excel Misc 3 3rd Oct 2006 07:06 PM
case Sensitive compare Rover Microsoft Access Queries 2 22nd Apr 2005 10:42 PM
Dictionary<K,V> and Case Insensitive key compare? William Stacey [MVP] Microsoft C# .NET 2 10th Aug 2004 07:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:33 PM.