运算符'=='不能应用于“System.Guid”类型的操作数;和“字符串”在 linq 到实体中
我收到此错误“运算符 '==' 无法应用于代码下面的 linq toEntityFramework 中类型为 'System.Guid' 和 'string'' 的操作数。 在下面的代码中,CustomerId 是 Guid,customerProfileId 是字符串。
var accountQuery = from C in CustomerModel.CustomerProfile
where C.CustomerId == customerProfileId // Error here
select C;
I am getting this error 'Operator '==' cannot be applied to operands of type 'System.Guid' and 'string'' in linq to entityframework below code.
in the below code CustomerId is Guid and customerProfileId is string.
var accountQuery = from C in CustomerModel.CustomerProfile
where C.CustomerId == customerProfileId // Error here
select C;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您不能直接将 Guid 与字符串进行比较。将字符串转换为 Guid 或将 Guid 转换为字符串。
将 Guid 转换为字符串就像在变量上调用
.ToString()
一样简单,但重要的是要知道格式化 Guid 的方法不止一种。无论有或没有破折号:someguid.ToString()
都会给你类似B06A6881-003B-4183-A8AB-39B51809F196
someGuid.ToString("N")
将返回类似B06A6881003B4183A8AB39B51809F196
的内容。如果您决定将
C.CustomerId
转换为字符串,请确保您知道什么格式customerProfileId
处于其中。如果它可以是任一格式,您最好进行转换
customerProfileId
到 guid:new Guid(customerProfileId)
。这样做的缺点是,如果格式不正确,从字符串到 Guid 的转换将引发异常。因此,如果您从用户输入(如表单字段或 URL)获取了
customerProfileId
,您应该首先对其进行验证。但是,如果将转换拉到查询之外,您可能会获得更好的性能,因为比较 Guid 可能比比较字符串更快。
You cannot compare a Guid to a string directly. Either convert the string to a Guid or the Guid to a string.
Converting a Guid to string is as easy as calling
.ToString()
on the variable, but it's important to know that there's more than one way to format the Guid. Either with or without dashes:someguid.ToString()
will give you something likeB06A6881-003B-4183-A8AB-39B51809F196
someGuid.ToString("N")
will return something likeB06A6881003B4183A8AB39B51809F196
If you decide to convert
C.CustomerId
to a string make sure you know what formatcustomerProfileId
is in.If it can be either format, you may be better off converting
customerProfileId
to a guid:new Guid(customerProfileId)
.The downside of this is that the conversion from string to Guid will throw an exception if it's not formatted correctly. So, if you got the
customerProfileId
from user input (like a form field or URL) you should validate it first.However, if you pull the conversion to Guid outside your query you'll probably end up with better performance since comparing Guids is probably faster than comparing strings.
那是因为你不能将 Guid 和字符串等同起来。
所以需要先将Guid转换为字符串。通常我会建议:
但是 Linq to Entities 中不存在
ToString()
。这个问题的答案 - 获取 GUID 字符串值时出现问题Linq-To-Entity 查询 - 可能会有所帮助。
That's because you can't equate a Guid and a string.
So you need to convert the Guid to string first. Normally I'd suggest:
but
ToString()
doesn't exist in Linq to Entities.The answer to this question - Problem getting GUID string value in Linq-To-Entity query - will probably help.
您必须将
CustomerId
转换为字符串(调用.ToString()
)或将customerProfileId
转换为Guid
(调用Guid.Parse()
),然后比较它们。You must convert
CustomerId
to string (calling.ToString()
) orcustomerProfileId
toGuid
(callingGuid.Parse()
) and then compare them.将其更改为:
或者将您的 customerProfileId 解析为 Guid 并在查询中使用它。
Change it to:
Or parse your customerProfileId to a Guid and use that in the query.
问题是什么?
显然,其中一个值是 Guid,而另一个值是字符串。您可以尝试以某种方式进行比较,如下所示:
C.CustomerId == new Guid(customerProfileId) 认为 C.CustomerId 是 Guid。
And what is the question?
Obviously, one of the values is Guid while another one is a string. You can try to compare somehow like this:
C.CustomerId == new Guid(customerProfileId) taking that C.CustomerId is a Guid.
您可以执行 C.CustomerId.toString() == customerProfileId 或用 new Guid(customerProfileId) 替换 customerProfileId
第二个应该更快,因为它唯一的一次转换和 guid 比较比字符串比较更快。
Can you do a C.CustomerId.toString() == customerProfileId or replace customerProfileId with new Guid(customerProfileId)
The second one should be faster as its only one conversion and guid compares are faster than string compares.
您需要从字符串生成新的 guid,它应该可以工作
you need to generate new guid from the string and it should work