实体框架Where方法参数
以下 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 似乎仅指不带参数的函数传递。
我缺少什么?
预先感谢,
瑞安
The following EF statement does not compile as it is expecting 2 additional parameters, an int and a bool. I can't figure out how to supply them or what they are used for.
Dim L2 = Db.SecurityLogs.Where(Function(x) x.OrderId = OrderId)
The compile error is;
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
I thought I understood how to use the Where method so I must be calling it differently to the way I normally do and MSDN only seems to refer to the passing of the function without parameters.
What am I missing?
Thanks in advance,
Ryan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为编译器错误消息中的最后一行(代表应该应用的相关重载)是重要的提示:
它指示在表达式
x.OrderId = OrderId
中,左侧(SecurityLog 类的 OrderId 属性)是可为 null 的 int (Integer?
) 或表达式中的类型右侧是一个可为 null 的 int。可空整数和不可空整数的比较会产生可空布尔值 (Boolean?
)。编译器抱怨它无法将此Boolean?
转换为 lambda 表达式所需的不可空Boolean
。要解决该问题,您可以将可空类型中可能的
Nothing
值转换为0
(或其他一些整数):或
(如果不是 100% 确定 VB 语法,但
If(x, y)
在此应对应于 C# 中的x ?? y
,因此:如果 x 为 Nothing,则返回 y,否则返回 x 的值。)还可以在 VB 编译器或项目设置中关闭选项Strict On,因为只有打开该选项时才会出现此错误。 (但我猜想,在您的项目中启用此选项可能还有其他一些原因,因为它不是默认的。)
I think the last line in the compiler error message (which represents the relevant overload which should be applied) is the important hint:
It indicates that in your expression
x.OrderId = OrderId
either the left side (OrderId property of your SecurityLog class) is a nullable int (Integer?
) or the type on the right side is a nullable int. Comparison of the a nullable and a non-nullable integer results in a nullable bool (Boolean?
). The compiler complains that it cannot convert thisBoolean?
to the non-nullableBoolean
which the lambda expression requires.To solve the problem you could convert the possible
Nothing
value in the nullable type to0
(or some other integer):or
(If not 100% sure about the VB syntax but
If(x, y)
should correspond here tox ?? y
in C#, so: if x is Nothing then return y, otherwise return the value of x.)You could also turn off the option Strict On in your VB compiler or project settings as this error only appears if the option is switched on. (But there might be some other reasons, I guess, to have this option on in your project, since it is non-default.)