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

前端 未结 4 1823
野的像风
野的像风 2021-01-04 02:49

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 perf

相关标签:
4条回答
  • 2021-01-04 03:38

    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

    0 讨论(0)
  • 2021-01-04 03:42

    An easy way to ignore case when doing a string comparison is just convert everything to lower (or upper) case before you compare it. So:

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

    That being said, I agree with the other commenters that you should be using a native GUID comparison, not a string comparison.

    0 讨论(0)
  • 2021-01-04 03:46

    Not sure why you're comparing them as text, but instead of t.user_id == userId use t.userId.Equals(userId, StringComparison.OrdinalIgnoreCase)

    0 讨论(0)
  • 2021-01-04 03:48

    I always had a dillema of using == for guid/uniqueidentifier columns comparison in linq statements (especially in where clause) so I took a rather safe route of using - (entityGuidColumn.CompareTo(guidParameter)==0) . This worked for me. Give it a shot.

    0 讨论(0)
提交回复
热议问题