【问题标题】:Operator '==' cannot be applied to operands of type 'System.Guid' and 'string' in linq to entity运算符 '==' 不能应用于 linq to entity 中类型为 'System.Guid' 和 'string' 的操作数
【发布时间】:2011-12-21 23:35:17
【问题描述】:

我在 linq to entityframework 下面的代码中收到此错误“运算符'=='不能应用于'System.Guid'和'string''类型的操作数。 在下面的代码中,CustomerId 是 Guid,customerProfileId 是字符串。

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

【问题讨论】:

    标签: c# .net linq entity-framework linq-to-entities


    【解决方案1】:

    您不能直接将 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 的转换,您最终可能会获得更好的性能,因为比较 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;
    

    【讨论】:

      【解决方案2】:

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

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

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

      ToString() 在 Linq to Entities 中不存在。

      这个问题的答案 - Problem getting GUID string value in Linq-To-Entity query - 可能会有所帮助。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          改成:

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

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

          【讨论】:

            【解决方案5】:

            问题是什么?

            显然,其中一个值是 Guid 而另一个是字符串。您可以尝试这样比较:

            C.CustomerId == new Guid(customerProfileId) 认为 C.CustomerId 是一个 Guid。

            【讨论】:

              【解决方案6】:

              你能做一个 C.CustomerId.toString() == customerProfileId 还是用新的 Guid(customerProfileId) 替换 customerProfileId

              第二个应该更快,因为它只有一个转换和 guid 比较比字符串比较快。

              【讨论】:

                【解决方案7】:
                var accountQuery = from C in CustomerModel.CustomerProfile
                              where C.CustomerId == new Guid(customerProfileId) // Error here                    
                             select C;
                

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

                【讨论】:

                  猜你喜欢
                  • 2017-01-19
                  • 1970-01-01
                  • 2013-10-12
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-07-31
                  • 1970-01-01
                  相关资源
                  最近更新 更多