问题
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
回答1:
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.)
来源:https://stackoverflow.com/questions/5633334/entity-framework-where-method-parameters