GetBytes方法无法转换为商店表达式

时间:2013-10-08 11:29:55

标签: c# sql linq entity-framework

我正在使用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的日期。如何更改?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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