我一直在尝试使用LINQ对我在下面定义的自定义类Aggregate_Table的IQueryable上执行求和。我不知道如何实现这一点,因为通常的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)
这是来自datacontext对象数据库中的表_58k_SIs的简单查询。由于此查询结果的类型是匿名数据类型,并且我需要创建一个接受特定表类型的函数,因此我继续创建一个类名称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对象列表,并将所有查询结果传输到该列表,因为list(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:\Users\chris\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 22 Active
我一直试图解决这个问题几个小时而无济于事。任何人都有任何建议我如何解决这个问题?
答案 0 :(得分:1)
您已将数量属性声明为Public Property Quantity
而非Public Property Quantity As Integer
。这会导致Quantity将结果强制转换为对象,并且没有可以汇总对象的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的值参数中包含类型,但如果编译器抱怨