实体框架Where方法参数

发布于 2024-10-31 18:05:50 字数 1975 浏览 0 评论 0原文

以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2024-11-07 18:05:50

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

扩展方法“公共函数”
其中(谓词 As
System.Linq.Expressions.Expression(Of
System.Func(SecurityLog, 布尔值)))
作为 System.Linq.IQueryable(Of
SecurityLog)' 定义于
“System.Linq.Queryable”:选项严格
On 不允许隐式转换
“布尔值?”为“布尔值”。

它指示在表达式 x.OrderId = OrderId 中,左侧(SecurityLog 类的 OrderId 属性)是可为 null 的 int (Integer?) 或表达式中的类型右侧是一个可为 null 的 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,因为只有打开该选项时才会出现此错误。 (但我猜想,在您的项目中启用此选项可能还有其他一些原因,因为它不是默认的。)

I think the last line in the compiler error message (which represents the relevant overload which should be applied) is the important hint:

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

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 this Boolean? to the non-nullable Boolean which the lambda expression requires.

To solve the problem you could convert the possible Nothing value in the nullable type to 0 (or some other integer):

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

or

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

(If not 100% sure about the VB syntax but If(x, y) should correspond here to x ?? 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文