views:

2339

answers:

2

I'm trying to inject a dynamic where clause in my Linq to SQL query and I get an overload exception. The same expression work when added in the query proper?

 qry.Where(Function(c) c.CallDate < Date.Now.AddDays(-1))

Any thoughts on how to this to work?

The exception reads:

Overload resolution failed because no accessible 'Where' can be called with these arguments:
Extension method 'Public Function Where(predicate As System.Func(Of Calls, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of Calls)' defined in 'System.Linq.Enumerable': Nested function does not have the same signature as delegate 'System.Func(Of Calls, Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Func(Of Calls, Boolean)) As System.Collections.Generic.IEnumerable(Of Calls)' 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 Calls, Integer, Boolean))) As System.Linq.IQueryable(Of Calls)' defined in 'System.Linq.Queryable': Nested function does not have the same signature as delegate 'System.Func(Of Calls, Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Calls, Boolean))) As System.Linq.IQueryable(Of Calls)' defined in 'System.Linq.Queryable': Option Strict On disallows implicit conversions from 'Boolean?' to 'Boolean'.  C:\Projects\Test Projects\Encore\EncoreData.vb 59 9 Encore

Thanks

+2  A: 

Just taking a stab here, haven't seen this before but here you go.

Looks like the key parts are these 2 lines from you exception:

Nested function does not have the same signature as delegate 'System.Func(Of Calls, Integer, Boolean)'.

disallows implicit conversions from 'Boolean?' to 'Boolean'.

So your function you are creating seems to have a output of type boolean? , whereas the where clause is expecting a boolean. And the system cannot do an implicit conversion from boolean? to boolean because of the possibility of a null value.

So, you might try writing your function with an explicit conversion. Either (boolean) or Convert.ToBool.

Carlton Jenke
+2  A: 

I found the answer...

c.Calldate is a nullable type, Date.Now is not nullable. The proper expression should read:

qry.Where(Function(c) c.CallDate.Value > Date.Now.AddDays(-1))

The hint in the exception was:

disallows implicit conversions from 'Boolean?' to 'Boolean'

Note the "?" after the first Boolean.

Thanks for making me take a closer look at the error message!

TGnat