How to compare two GUIDs in Linq to Entities

自闭症网瘾萝莉.ら 提交于 2019-12-11 12:14:17

问题


I am writing an L2E query to compare two GUID values.It simply doesn't allow direct comparison, and also .ToString() method is not allowed on L2E queries.. How can we achieve this?


回答1:


I don't know if this applies to your case, but I found that I can use the Guid.CompareTo method in Linq, and it properly converts this to SQL.

documentQuery.Where(s => s.DocumentGuid.CompareTo(MyGuidVariable) > 0);

This produces the following SQL:

AND ([Extent1].[DocumentGuid] > @p__linq__1)



回答2:


you need to compare string represantation of both the GUID parameters

string var1=Convert.ToString(GuidParam1);
string var2=Convert.ToString(GuidParam2)

then compare those string values

   if(var1.CompareTo(var2)==0)


来源:https://stackoverflow.com/questions/14116986/how-to-compare-two-guids-in-linq-to-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!