运算符'=='不能应用于“System.Guid”类型的操作数;和“字符串”在 linq 到实体中

发布于 2024-12-13 19:49:33 字数 347 浏览 0 评论 0原文

我收到此错误“运算符 '==' 无法应用于代码下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

安稳善良 2024-12-20 19:49:33

您不能直接将 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 可能比比较字符串更快。

var customerProfileGuid = new Guid(customerProfileId);  
// wrap in try catch if needed

var accountQuery = from C in CustomerModel.CustomerProfile
                   where C.CustomerId == customerProfileGuid                    
                   select C;

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 like B06A6881-003B-4183-A8AB-39B51809F196
someGuid.ToString("N") will return something like B06A6881003B4183A8AB39B51809F196

If you decide to convert C.CustomerId to a string make sure you know what format customerProfileId 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.

var customerProfileGuid = new Guid(customerProfileId);  
// wrap in try catch if needed

var accountQuery = from C in CustomerModel.CustomerProfile
                   where C.CustomerId == customerProfileGuid                    
                   select C;
_蜘蛛 2024-12-20 19:49:33

那是因为你不能将 Guid 和字符串等同起来。

所以需要先将Guid转换为字符串。通常我会建议:

where C.CustomerId.ToString().Equals(customerProfileId)

但是 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:

where C.CustomerId.ToString().Equals(customerProfileId)

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.

泪是无色的血 2024-12-20 19:49:33

您必须将 CustomerId 转换为字符串(调用 .ToString())或将 customerProfileId 转换为 Guid(调用 Guid.Parse()),然后比较它们。

You must convert CustomerId to string (calling .ToString()) or customerProfileId to Guid (calling Guid.Parse()) and then compare them.

云归处 2024-12-20 19:49:33

将其更改为:

var accountQuery = from C in CustomerModel.CustomerProfile
                  where C.CustomerId.ToString() == customerProfileId                
                 select C;

或者将您的 customerProfileId 解析为 Guid 并在查询中使用它。

Change it to:

var accountQuery = from C in CustomerModel.CustomerProfile
                  where C.CustomerId.ToString() == customerProfileId                
                 select C;

Or parse your customerProfileId to a Guid and use that in the query.

毁梦 2024-12-20 19:49:33

问题是什么?

显然,其中一个值是 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.

披肩女神 2024-12-20 19:49:33

您可以执行 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.

心在旅行 2024-12-20 19:49:33
var accountQuery = from C in CustomerModel.CustomerProfile
              where C.CustomerId == new Guid(customerProfileId) // Error here                    
             select C;

您需要从字符串生成新的 guid,它应该可以工作

var accountQuery = from C in CustomerModel.CustomerProfile
              where C.CustomerId == new Guid(customerProfileId) // Error here                    
             select C;

you need to generate new guid from the string and it should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文