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

DenaliHardtail picture DenaliHardtail · Apr 20, 2011 · Viewed 33.3k times · Source

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.

Answer

Aaron Anodide picture Aaron Anodide · Apr 20, 2011

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