【发布时间】:2014-09-24 08:04:22
【问题描述】:
请考虑以下代码:
Public MustInherit Class BaseClassHandler
Public MustInherit Class MyOwnBaseClass
Inherits TableEntity
Public Property Val As Integer
End Class
Protected Function BuildFilter(PK As String, RK As String) As Expressions.Expression(Of Func(Of MyOwnBaseClass, Boolean))
Return Function(entity) entity.PartitionKey = PK And entity.RowKey = RK
End Function
End Class
Public Class ClassHandler_A
Inherits BaseClassHandler
Public Class MyOwnClass_A
Inherits BaseClassHandler.MyOwnBaseClass
Public Property Val_A As Integer
Public Sub New()
End Sub
End Class
Public Function RetrieveEntity(Table As CloudTable, RowKey As String, PartitionKey As String) As MyOwnClass_A
' Following line gives me a build error
Return Table.CreateQuery(Of MyOwnClass_A).Where(MyBase.BuildFilter(RowKey, PartitionKey)).SingleOrDefault()
End Function
End Class
Public Class ClassHandler_B
Inherits BaseClassHandler
Public Class MyOwnClass_B
Inherits BaseClassHandler.MyOwnBaseClass
Public Property Val_B As Integer
Public Sub New()
End Sub
End Class
Public Function RetrieveEntity(Table As CloudTable, RowKey As String, PartitionKey As String) As MyOwnClass_B
' Following line gives me a build error
Return Table.CreateQuery(Of MyOwnClass_B).Where(MyBase.BuildFilter(RowKey, PartitionKey)).SingleOrDefault()
End Function
End Class
这里我有 2 个基类,每个类型由其他两个类继承。 我想要完成的是有一个可以构建过滤器的基本方法,然后在派生类中使用。
当然,前面报告的代码给了我一个错误,因为过滤器是“构建”MyOwnBaseClass 而不是它的任何派生过滤器:
Error 1 Overload resolution failed because no accessible `Where` can be called with these arguments:
[...]
Extension method `Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of MyOwnClass_A, Boolean))) As System.Linq.IQueryable(Of MyOwnClass_A)` defined in `System.Linq.Queryable`: Value of type `System.Linq.Expressions.Expression(Of System.Func(Of Test.Form1.BaseClassHandler.MyOwnBaseClass, Boolean))` cannot be converted to `System.Linq.Expressions.Expression(Of System.Func(Of Test.Form1.ClassHandler_A.MyOwnClass_A, Boolean))`
[...]
有没有办法实现我的目的无需自己构建过滤器字符串?
我无法添加 C# 标记,因为它会以 C# 方式重新着色 VB 发布的代码,但我也会在答案中接受 C#。
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
好吧,我很抱歉。
标签: .net vb.net linq azure expression