GetBytes 方法不能转换为存储表达式

本文关键字:存储 表达式 转换 方法 不能 GetBytes | 更新日期: 2023-09-27 18:34:27

我正在使用LINQ根据特定条件选择列表。属性值存储在 byte array 中,稍后在存储在数据库表中时对其进行加密。我现在想在我的SELECT LINQ查询中使用此属性,但是当我尝试这样做时,它会引发以下异常:

LINQ to Entities does not recognize the method 'Byte[] GetBytes(System.String)' method, and this method cannot be translated into a store expression.

这是我使用的代码:

var result = (from history in context.Histories
                              where history.ID == Id & 
                              (history.Salary != null || history.Salary != Encoding.ASCII.GetBytes("0"))
                              select (DateTime?)history.Date).Max();
                return result;

我想从历史记录表中选择工资不等于 null 或 0 的 id 的日期。我该如何更改此设置?

GetBytes 方法不能转换为存储表达式

先获取字节:

var bytes = Encoding.ASCII.GetBytes("0");
var result = (from history in context.Histories
                              where history.ID == Id & 
                          (history.Salary != null || history.Salary != bytes)
                          select (DateTime?)history.Date).Max();
            return result;

将代码更改为:

var bytes = Encoding.ASCII.GetBytes("0");
var result = (from history in context.Histories
                          where history.ID == Id & 
                          (history.Salary != null || history.Salary != bytes)
                          select (DateTime?)history.Date).Max();
            return result;

LINQ 在将查询转换为 SQL 时无法计算 GetBytes