如何使用 LINQ 对 GUID 进行不区分大小写的比较?
在下面的代码中,我想比较两个 GUID。问题是我没有收到任何任务返回,因为 GUIDS 的大小写不同(大写与小写)。我需要执行不区分大小写的比较。
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();
如何比较 GUID 并查找匹配项(无论大小写)?
更新1 现在将会员资格提供商的 GUID 转换为 GUID
Guid userId = (Guid) membershipUser.ProviderUserKey;
但我仍然没有得到任何匹配项。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Guid 上的 == 已重载,因此您无需比较字符串表示形式。
请参阅 http://msdn.microsoft.com/en- us/library/system.guid.op_equality(v=VS.90).aspx
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
不知道为什么要将它们作为文本进行比较,而不是使用
t.user_id == userId
使用t.userId.Equals(userId, StringComparison.OrdinalIgnoreCase)
Not sure why you're comparing them as text, but instead of
t.user_id == userId
uset.userId.Equals(userId, StringComparison.OrdinalIgnoreCase)
我总是在 linq 语句中使用 == 进行 guid/uniqueidentifier 列比较(尤其是在 where 子句中),因此我采取了一种相当安全的使用方法 - (entityGuidColumn.CompareTo(guidParameter)==0) 。这对我有用。试一试。
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.
在进行字符串比较时忽略大小写的一个简单方法是在比较之前将所有内容转换为小写(或大写)。所以:
话虽如此,我同意其他评论者的观点,即您应该使用本机 GUID 比较,而不是字符串比较。
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:
That being said, I agree with the other commenters that you should be using a native GUID comparison, not a string comparison.