我编写了以下函数来检查数据库中重叠时间段的添加/修改记录:
<Extension> Function ClashRecords(Of T)(Query As IQueryable(Of T), Record As T, _
KeySelector As Expression(Of Func(Of T, Integer)),
FromSelector As Expression(Of Func(Of T, DateTime)),
TillSelector As Expression(Of Func(Of T, DateTime))) As IQueryable(Of T)
Dim key = KeySelector.Invoke(Record)
Dim fromDate = FromSelector.Invoke(Record)
Dim tillDate = TillSelector.Invoke(Record)
Dim criteriaExpr As Expression(Of Func(Of T, Boolean)) = Function(x) KeySelector.Invoke(x) = key And FromSelector.Invoke(x) <= tillDate And TillSelector.Invoke(x) >= fromDate
Return Query.AsExpandable.Where(criteriaExpr.Expand)
End Function
调用函数时如下:
Dim de As New DataEntities()
Dim w=New Work With {.WorkerID=-1,.FromDate=New DateTime(2014,3,20,7,0,0),.TillDate=New DateTime(2014,3,20,8,30,0)}
Dim clashing = de.Works.ClashRecords(w,Function(x) x.ActivistID, Function(x) x.FromDate, Function(x) x.TillDate)
我收到以下错误:
InvalidCastException:无法将“System.Linq.Expressions.FieldExpression”类型的对象转换为“System.Linq.Expressions.LambdaExpression”类型。
我没有看到我正在使用任何字段表达式—— ActivistID
, FromDate
,TillDate
都是属性。
我正在使用 EF5 并生成 POCO 类。
我该如何解决这个问题?
更新
如果我criteriaExpr.Expand
在 Watch 窗口中输入,则不会出现异常。但是,如果我输入,criteriaExpr.Expand.Compile
我会得到同样的异常。