Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform a case-insensitive compare of GUIDs with LINQ?

In the code below, I want to compare two GUIDs. The problem is I don't get any tasks returned because the GUIDS are different case (uppercase vs. lowercase). I need to perform a case-insensitive compare.

MembershipUser membershipUser = Membership.GetUser();
string strUserId = membershipUser.ProviderUserKey.ToString();

Guid userId = new Guid(strUserId.ToUpper());

lblUserId.Text = userId.ToString();

DataModelEntities dc = new DataModelEntities();

var userTasks = dc.tasks.Where(t => t.user_id == userId).ToList();

How do I compare the GUIDs and find matches regardless of case?

UPDATE 1 now coverting the guid out of the membership provider to a GUID

Guid userId = (Guid) membershipUser.ProviderUserKey;

BUt I'm still not getting any matches.

like image 375
DenaliHardtail Avatar asked Apr 20 '11 02:04

DenaliHardtail


People also ask

How do you do case insensitive string comparison?

Comparing strings in a case insensitive manner means to compare them without taking care of the uppercase and lowercase letters. To perform this operation the most preferred method is to use either toUpperCase() or toLowerCase() function.

How do I compare two GUIDs?

The CompareTo method compares the GUIDs as if they were values provided to the Guid constructor, as follows: It compares the Int32 values, and returns a result if they are unequal. If they are equal, it performs the next comparison. It compares the first Int16 values, and returns a result if they are unequal.

Is Linq where clause case-sensitive?

LINQ has no concept of case sensitivity, it only cares about boolean evaluation. So if you want to ignore case, you should do something like: query = query.

Is comparing strings case-sensitive?

CompareTo and Compare(String, String) methods. They all perform a case-sensitive comparison.


1 Answers

The == is overloaded on Guid so you don't need to compare the string representations.

See http://msdn.microsoft.com/en-us/library/system.guid.op_equality(v=VS.90).aspx

like image 66
Aaron Anodide Avatar answered Oct 14 '22 06:10

Aaron Anodide