【问题标题】:LINQ to Entities does not recognize the method 'Boolean ToBoolean [duplicate]LINQ to Entities 无法识别方法“Boolean ToBoolean [重复]
【发布时间】:2015-12-20 13:39:57
【问题描述】:

我有这样的课:

public class menu{
public string Permission{get;set;}
}

Permission 的值是 Encripted 。我想要PermissionTrue 的所有记录。为此,我使用以下查询:

return 
_menuSettings.Where(row => Convert.ToBoolean(Utilities.Encryption.Decrypt(row.Permission,"key"))==true).ToList();

但我收到此错误:

LINQ to Entities 无法识别方法 'Boolean ToBoolean(System.String)' 方法,并且该方法无法转换为存储表达式。

我在谷歌上搜索过,但我无法解决。

谢谢

【问题讨论】:

标签: c# entity-framework linq


【解决方案1】:

数据库查询无法实现您所要求的。恐怕您会陷入这样的内存过滤(希望您没有太多记录)

return 
_menuSettings.AsEnumerable().Where(...

这里AsEnumerable() 会将上下文从Linq to Entities 切换到Linq to Objects

【讨论】:

    【解决方案2】:

    并非所有方法都可以转换为 SQL,这就是该消息的本质。

    在您的情况下,您可以与字符串 "true" 进行比较。

    _menuSettings.Where(row => Utilities.Encryption.Decrypt(row.Permission,"key").ToLower()=="true").ToList();
    

    现在如前所述,消息意味着该方法不能转换为 SQL。所以毫不奇怪,Utilities.Encryption.Decrypt 也不支持。

    然后继续使用相同的概念,将不工作的东西从查询中剔除。

    快速而肮脏的方法是实现/投影数据(在使用不支持的过滤器进行过滤之前使用 ToList() 或 ToIEnumerable())。

    这意味着您从表中取出所有内容并在您的服务器上而不是在 DBMS(SQL 服务器)上对其进行过滤。

    像这样。 (为了便于阅读,我把它分成了更多行)

    var projection = _menuSettings.ToList();
    var result = projection.Where(row => Utilities.Encryption.Decrypt(row.Permission,"key").ToLower()=="true").ToList();
    

    在进行此类繁重的工作之前,找到一种限制投影尺寸的好方法是一个明智的选择。

    【讨论】:

    • 现在他会得到Utilities.Encryption.Decrypt not supported
    • 我收到此错误LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method, and this method cannot be translated into a store expression.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多