【问题标题】:Entity Framework Where method parameters实体框架 Where 方法参数
【发布时间】:2011-04-12 09:51:09
【问题描述】:

以下 EF 语句无法编译,因为它需要 2 个附加参数,一个 int 和一个 bool。我不知道如何提供它们或它们的用途。

Dim L2 = Db.SecurityLogs.Where(Function(x) x.OrderId = OrderId)

编译错误是;

Error   27  Overload resolution failed because no accessible 'Where' can be called with these arguments:
'Public Function Where(predicate As String, ParamArray parameters() As System.Data.Objects.ObjectParameter) As System.Data.Objects.ObjectQuery(Of SecurityLog)': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type.
Extension method 'Public Function Where(predicate As System.Func(Of SecurityLog, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of SecurityLog)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of SecurityLog, Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Func(Of SecurityLog, Boolean)) As System.Collections.Generic.IEnumerable(Of SecurityLog)' defined in 'System.Linq.Enumerable': Option Strict On disallows implicit conversions from 'Boolean?' to 'Boolean'.
Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of SecurityLog, Integer, Boolean))) As System.Linq.IQueryable(Of SecurityLog)' defined in 'System.Linq.Queryable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of SecurityLog, Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of SecurityLog, Boolean))) As System.Linq.IQueryable(Of SecurityLog)' defined in 'System.Linq.Queryable': Option Strict On disallows implicit conversions from 'Boolean?' to 'Boolean'.  D:\Projects\OutdoorAndCountry\trunk\Source\Server\DataModel\SecurityLogDb.vb    24  22  DataModel

我以为我理解了如何使用 Where 方法,所以我必须以不同于通常的方式调用它,而 MSDN 似乎只是指不带参数的函数的传递。

我错过了什么?

提前致谢,

瑞恩

【问题讨论】:

    标签: entity-framework-4 where


    【解决方案1】:

    我认为编译器错误消息的最后一行(代表应该应用的相关重载)是重要的提示:

    扩展方法'公共函数 哪里(谓词作为 System.Linq.Expressions.Expression(的 System.Func(Of SecurityLog, Boolean))) 作为 System.Linq.IQueryable(的 SecurityLog)' 定义在 'System.Linq.Queryable':选项严格 On 禁止从 “布尔值?”到“布尔”。

    这表明在您的表达式x.OrderId = OrderId 中,左侧(SecurityLog 类的OrderId 属性)是可为空的int (Integer?),或者右侧的类型是可为空的int。可空整数和不可空整数的比较产生可空布尔 (Boolean?)。编译器抱怨它无法将此 Boolean? 转换为 lambda 表达式所需的不可为空的 Boolean

    为了解决这个问题,您可以将可能的可空类型中的Nothing 值转换为0(或其他整数):

    ' If x.OrderId is the nullable Integer
    Dim L2 = Db.SecurityLogs.Where(Function(x) If(x.OrderId, 0) = OrderId)
    

    ' If OrderId is the nullable Integer
    Dim L2 = Db.SecurityLogs.Where(Function(x) x.OrderId = If(OrderId, 0))
    

    (如果不是 100% 确定 VB 语法,但 If(x, y) 应该在此处对应于 C# 中的 x ?? y,因此:如果 x 为 Nothing,则返回 y,否则返回 x 的值。)

    您也可以在 VB 编译器或项目设置中关闭选项 Strict On,因为此错误仅在打开该选项时才会出现。 (但我猜可能还有其他一些原因,在您的项目中启用此选项,因为它是非默认的。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多