LINQ to Entities不能识别'System方法.字符串解密(系统.system . String)字
本文关键字:解密 字符串 系统 system String 方法 System 不能 Entities to 识别 | 更新日期: 2023-09-27 18:08:11
我试图通过使用电子邮件过滤在combobox
中显示电子邮件。我的问题是我的数据在users表中被加密了。
当我试图解密它时,它给出了这个错误:
LINQ to Entities不能识别方法System。字符串解密(系统。字符串,System.String)'方法,并且该方法不能转换为存储表达式
如何解决这个错误?
这是我的查找类
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;
}
记住,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)),
}));
我已经设法得到一个解决方案,这只是一个小问题。我应该包括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)),
}));