我一直试图在vb.net上使用LINQ对我在下面定义的自定义类Aggregate_Table的IQueryable执行sum。我不确定如何实现这一目标,因为通常的sum函数在应用于我的自定义类的IQueryable(Of T)时给了我语法错误。
我从下面的查询开始:
Dim initial_SIs = From si In database._58k_SIs
Group By si.From_Firm, si.From_Account_Number, si.To_Account_Number, si.To_Firm, si.Security_Code, si.Settlement_Ccy
Into Quantity = Sum(si.Quantity), Settlement_Amount = Sum(si.Settlement_Amount)
这是对数据上下文对象数据库中的表_58k_si的简单查询。由于此查询结果的类型是匿名数据类型,并且我需要创建一个接受特定表类型的函数,因此我继续创建一个类名Aggregate_SI,它具有与上面查询结果相同的属性(该类的每个属性都有自己相应的属性,为了简洁起见,我省略了这些属性)
Public Class Aggregated_SI
Private _From_Firm As String
Private _From_Account_Number As String
Private _To_Account_Number As String
Private _To_Firm As String
Private _Security_Code As String
Private _Quantity As Integer
Private _Settlement_Amount As Decimal
Private _Settlement_Ccy As String
Public Property Quantity
Get
Return _Quantity
End Get
Set(value)
_Quantity = value
End Set
End Property
End Class
我创建了一个Aggregated_SI对象列表,并将所有查询结果传输到该列表,因为列表(of T)对象可以用于LINQ查询。
Dim test_List = New List(Of Aggregated_SI)
For Each si In initial_SIs
test_List.Add(New Aggregated_SI(si.From_Firm, si.From_Account_Number, si.To_Account_Number, si.To_Firm, si.Security_Code, si.Quantity, si.Settlement_Amount, si.Settlement_Ccy))
Next
聚合:
Dim outflow = From si In test_List
Group By si.From_Account_Number, si.Security_Code
Into Total_outflow = Sum(si.Quantity)
给出错误信息
Error BC36594 Definition of method 'Sum' is not accessible in this context.
Error BC30519 Overload resolution failed because no accessible 'Sum' can be called without a narrowing conversion:
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Integer)) As Integer' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Integer'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Integer?)) As Integer?' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Integer?'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Long)) As Long' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Long'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Long?)) As Long?' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Long?'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Single)) As Single' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Single'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Single?)) As Single?' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Single?'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Double)) As Double' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Double'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Double?)) As Double?' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Double?'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Decimal)) As Decimal' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Decimal'.
Extension method 'Public Function Sum(selector As Func(Of Aggregated_SI, Decimal?)) As Decimal?' defined in 'Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Decimal?'. Settlement_Optimisation C:UserschrisAppDataLocalTemporary ProjectsWindowsApplication1Form1.vb 22 Active
我已经试着解决这个问题几个小时了,但无济于事。谁有什么建议,我该如何解决这个问题?
您已经将Quantity属性声明为Public Property Quantity
而不是Public Property Quantity As Integer
。这导致Quantity将结果强制转换为object,并且没有Sum()扩展可以汇总对象。使用正确的返回类型,它应该可以工作:
Public Class Aggregated_SI
Private _From_Firm As String
Private _From_Account_Number As String
Private _To_Account_Number As String
Private _To_Firm As String
Private _Security_Code As String
Private _Quantity As Integer
Private _Settlement_Amount As Decimal
Private _Settlement_Ccy As String
Public Property Quantity As Integer
Get
Return _Quantity
End Get
Set(value As Integer)
_Quantity = value
End Set
End Property
End Class
PS:我写VB已经很久了。Net,我不确定是否包括setter的值参数中的类型,但如果编译器抱怨