运算符'=='不能在LINQ应用于类型的操作数'的System.Guid'和'串'为单位 [英] Operator '==' cannot be applied to operands of type 'System.Guid' and 'string' in linq to entity

查看:156
本文介绍了运算符'=='不能在LINQ应用于类型的操作数'的System.Guid'和'串'为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误'操作'=='不能应用于类型的操作数'的System.Guid和在LINQ字符串''来的EntityFramework低于code。 在下面的code客户ID是GUID和customerProfileId是字符串。

  VAR accountQuery =从C在CustomerModel.CustomerProfile
                  其中,C.CustomerId == customerProfileId //这里出错
                 选择C;
 

解决方案

您不能直接比较一个GUID来一个字符串。无论是串一个GUID或将GUID转换为字符串。

转换一个GUID字符串是变容易,因为调用的ToString(),但重要的是要知道,有格式化的GUID的方法不止一种。具有或不破折号:

someguid.ToString()会给你像 B06A6881-003B-4183-A8AB-39B51809F196 someGuid.ToString(N)将返回类似 B06A6881003B4183A8AB39B51809F196

如果您决定 C.CustomerId 转换为字符串,确保你知道什么样的格式 customerProfileId 的研究。

如果它可以是任何一种格式,你可能会更好转换 customerProfileId 来一个GUID:新的GUID(customerProfileId)

这样做的缺点是,如果它的格式不正确的字符串的Guid转换将抛出一个异常。所以,如果你得到了 customerProfileId 用户输入(如表单字段,或URL),你应该先验证它。

不过,如果你拉转换的Guid查询外,你可能会最终提供更好的性能,因为比较的GUID可能比比较字符串快。

  VAR customerProfileGuid =新的GUID(customerProfileId);
//如果需要包装在尝试捕捉

VAR accountQuery =从C在CustomerModel.CustomerProfile
                   其中,C.CustomerId == customerProfileGuid
                   选择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;

解决方案

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;

这篇关于运算符'=='不能在LINQ应用于类型的操作数'的System.Guid'和'串'为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆