我的应用程序是MVC 5,使用EF 6.3。我正在尝试使用以下方法解密两列:
var grid = db.mytable.Where(c => c.Item== "Order").Select(c => new
{
FirstName = Encryption.Decrypt(c.FirstName),
LastName = Encryption.Decrypt(c.LastName),
Id = c.Id
}).ToList();
我收到此错误:LINQ to Entities无法识别方法'System.String Decrypt(System.String)',并且无法将该方法转换为存储表达式。
Select
中的代码转换为SQL,但没有将Encryption.Decrypt
方法转换为SQL。相反,您需要执行查询,然后对结果集进行解密。这可以通过在ToList()
之前加Select
强制执行来完成
var grid = db.mytable.Where(c => c.Item== "Order").ToList().Select(c => new
{
FirstName = Encryption.Decrypt(c.FirstName),
LastName = Encryption.Decrypt(c.LastName),
Id = c.Id
}).ToList();
.ToList()
,使您的查询成为Immediate Query Execution。之后,您可以根据需要执行操作。var filteredTable = db.mytable.Where(c => c.Item== "Order").ToList();
var grid = filteredTable .Select(c => new
{
FirstName = Encryption.Decrypt(c.FirstName),
LastName = Encryption.Decrypt(c.LastName),
Id = c.Id
}).ToList();