2016-09-20 99 views
2

我试图通过使用电子邮件过滤在combobox中显示电子邮件。我的问题是我的数据在users表中被加密。LINQ to Entities无法识别方法'System.String Decrypt(System.String,System.String)'方法

当我试图解密它,它给这个错误:

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

我怎样才能解决这个问题?

这里是我的Lookup类

public class Lookup 
    { 
     public long boundvalue { get; set; } 
     public string boundtext { get; set; } 
    } 

这里是我的代码过滤

public IEnumerable<Lookup> getUser(string fText) 
      { 
       var ret = new List<Lookup> 
           { 
            new Lookup 
             { 
              boundvalue = 0, 
              boundtext = "" 
             } 
           }; 
       if (!string.IsNullOrEmpty(fText)) 
       { 
        ret.AddRange(_entities.Users.Where(x =>EncDec.Decrypt(x.UserVar01.Trim().Replace("_",string.Empty), 
        Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)).Contains(fText.Trim())) 
           .Select(select => new Lookup 
           { 
            boundvalue = select.UserID, 
            boundtext = EncDec.Decrypt(select.UserVar01.Trim().Replace("_", string.Empty), 
            Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)), 
           })); 

       } 
       return ret; 
      } 
+0

未使用过'String.Encrypt'和'String.Decrypt'。但是在查询之前加密*'fText'而不是*解密*'x.UserVar01'呢? –

回答

0

。 我是假设包括ToList()

ret.AddRange(_entities.Users.ToList().Where(x =>EncDec.Decrypt(x.UserVar01.Trim().Replace("_",string.Empty), 
        Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)).Contains(fText.Trim())) 
           .Select(select => new Lookup 
           { 
            boundvalue = select.UserID, 
            boundtext = EncDec.Decrypt(select.UserVar01.Trim().Replace("_", string.Empty), 
            Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)), 
           })); 

现在,它的工作。

+2

请注意,您正在实现表用户的**完整内容**。只有在行数很少的情况下才应该这样做。您可以做的优化是仅选择您需要的列。 – Maarten

2

记住一个LINQ到年底实体查询转换到SQL格式,但什么错误是说当时不支持System.String Decrypt做翻译。这就是为什么我担心你不能在服务器端应用过滤器,你需要在内存中执行。要做到这一点使用AsEnumerable扩展方法,使开关到LINQ到我已经得到了一个解决方案,这只是一个小问题,对象

ret.AddRange(_entities.Users.AsEnumerable().Where(x =>EncDec.Decrypt(x.UserVar01.Trim().Replace("_",string.Empty), 
       Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)).Contains(fText.Trim())) 
          .Select(select => new Lookup 
          { 
           boundvalue = select.UserID, 
           boundtext = EncDec.Decrypt(select.UserVar01.Trim().Replace("_", string.Empty), 
           Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)), 
          })); 
+1

强烈建议在加密内容外添加一些过滤功能,这样您就不必像写入一样将所有记录全部拉回。然而,这是正确的基础上发布的过滤,所以没有敲的答案,只是FYI OP你应该添加一些东西,所以你可以过滤而不先解密。 – Tim

相关问题