views:

262

answers:

6

I'm reading a username and then checking to see if exists in another database table, the problem is whilst the username is the same the case maybe different and is preventing it from finding a match example jsmith and JSmith or JSMITH.

How can I fix this? Should I lower the case when writing to the first database or can I alter my code below when I'm comparing the two?

drUser["Enrolled"] = 
    (enrolledUsers.FindIndex(x => x.Username == (string)drUser["Username"]) != -1);

UPDATE:

Still struggling with this, the code below compiles but doesn't give the correct result, when viewing enrolled users I see those that aren't enrolled, when viewing those that are not enrolled I see 1 that is enrolled but their username case is the same in each datababse. Have I formatted the code below correctly?

drUser["Enrolled"] = (enrolledUsers.FindIndex(x => x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)));

Thanks Jamie

+19  A: 

You need to cal the Equals method, which takes a StringComparison parameter.

For example:

x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)

If x.Username can be null, you should call the static Equals method:

String.Equals(x.Username, (string)drUser["Username"], StringComparison.OrdinalIgnoreCase)

Otherwise, x.Username.Equals can throw a NullReferenceException.

SLaks
Sorry to sound thick but the code below doesn't work correctly, it compiles but doesn't find the matching users.drUser["Enrolled"] = (enrolledUsers.FindIndex(x => x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)));
Jamie
Just tried the update, and this doesn't compile. Where am I going wrong?drUser["Enrolled"] = (enrolledUsers.FindIndex(String.Equals(x.Username, (string)drUser["Username"], StringComparison.OrdinalIgnoreCase)));
Jamie
Please edit your question and add your exact code.
SLaks
+3  A: 

The preferred way to do this, is to specify the string comparison by using something like

string.Equals(x.Username, (string)drUser["Username"], StringComparison.OrdinalIgnoreCase

to do the equality check, instead of "=="

Rob Levine
This won't compile (you forgot to cast).
SLaks
good spot - thanks!
Rob Levine
A: 

You should use the String.Compare method

drUser["Enrolled"] = (enrolledUsers.FindIndex(x => 
                        string.Compare(x.Username,(string)drUser["Username"], false) == 0) != -1);
jmoreno
Yes, but why? Use `Equals` instead.
SLaks
Because it introduces him to more of the .Net Framework. That's why.
jmoreno
A: 

How about using ToUpper().

 if(!(dr["Enrolled"] == null || dr["Username"] == null))
 {
    if(dr["Enrolled"].ToString().ToUpperInvariant()== dr["Username"].ToString().ToUpperInvariant())
    {
        //Do Something
    }
}
MikeTWebb
please provide a reason for the down vote. I'd like to learn why this code is a bad practice. Thanks
MikeTWebb
I didn't downvote you, but I can guess why. Firstly, this will explode if either value is null. Secondly, this is not culture aware. It may work for English (and lots of other languages too), but not all. The oft-quoted example is "The Turkish Case": http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html .Net gives you culturally aware string comparisons for free - use them!
Rob Levine
Far out....thanks for the explanation. Much appreciated. Yeah, as far as the NULLs....the code example wasn't intended to be bullet proof. I always check for NULLs.
MikeTWebb
Side note: `ToUpperInvariant` would be your way to go then.
Markus
As a side note, ToUpper also creates a whole new string, which is extra overhead. It isn't a big deal for a little string here or there, but if you are doing a lot of comparisons it seems a bit wasteful to create a temporary just to do the compare.
Dolphin
@Dolphin...roger that
MikeTWebb
A: 

Have you tried this?

string userName = (string)drUser["Username"];
bool enrolled = enrolledUsers.Exists(x =>
  string.Equals(x.Name, userName, StringComparison.CurrentCultureIgnoreCase));

Is there a reason you are using FindIndex instead?

Andre Artus
A: 

try the string.compare method. all overloads

Or a more specific one

If nothing else, I hope it educates.

paul