score:2

Accepted answer
public virtual User GetUser(string username)
        {
            return _db.Users.Single(x => x.UserName.ToLower() == username.ToLower());

        }

But I don't think the compare will be much slower though....

score:10

String.Compare is not in the list of functions supported by entity framework (see Supported Function List) This means that when you perform this query entity framework will retrieve the whole data set required to execute this compare and perform the compare locally. This will be very slow.

A much better solution is to use == to compare strings, for example:

return _db.Users.Single(x => x.UserName == username);

score:4

I would use String.Equals

return _db.Users.Single(x => 
          String.Equals(x.UserName, username, StringComparer.OrdinalIgnoreCase))

If i want to match "Martín" with "martin" (accent) as same, I would use String.Compare.

return _db.Users.Single(x => 
         string.Compare(x.UserName, username, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);

Related Articles