0

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 的日期。我该如何更改?

4

2 回答 2

4

只需先获取字节:

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;
于 2013-10-08T11:33:19.920 回答
2

将您的代码更改为:

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

于 2013-10-08T11:34:24.300 回答