【问题标题】:GetBytes method cannot be translated into a store expressionGetBytes 方法无法转换为存储表达式
【发布时间】:2013-10-08 11:29:54
【问题描述】:

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

【问题讨论】:

    标签: c# sql linq entity-framework


    【解决方案1】:

    先获取字节:

    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;
    

    【讨论】:

    • 谢谢!像魅力一样工作!
    【解决方案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

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多