Entity framework 4 实体框架,其中包含方法参数

Entity framework 4 实体框架,其中包含方法参数,entity-framework-4,where,Entity Framework 4,Where,以下EF语句不编译,因为它需要两个附加参数,一个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

以下EF语句不编译,因为它需要两个附加参数,一个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似乎只是指没有参数的函数传递

我错过了什么

提前感谢,


Ryan

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

可拓方法的公共功能 Where(谓词As) System.Linq.Expressions.Expression(Of System.Func(用于SecurityLog,Boolean))) As系统Linq.IQueryable(Of 中定义的安全日志“” “System.Linq.Queryable”:选项严格 不允许从中进行隐式转换 “布尔”改为“布尔”

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

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

(如果不能100%确定VB语法,但
If(x,y)
应与C#中的
x??y
对应,因此:如果x为零,则返回y,否则返回x的值。)

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

' 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))