【问题标题】:Performing aggregate sum on custom class IQueryable(Of T)对自定义类 IQueryable(Of T) 执行聚合求和
【发布时间】:2016-07-05 08:42:21
【问题描述】:

我一直在尝试在 vb.net 上使用 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

我已经尝试解决这个问题几个小时,但无济于事。有人对我该如何解决这个问题有任何建议吗?

【问题讨论】:

  • 请包含 Aggregated_SI 类的完整定义。您只包含了字段声明(私有),但显然您正在使用属性 Quantity 并且您没有显示它的定义。从错误中我假设 Quantity 属性返回一个对象而不是整数。
  • 我已经包含了数量属性,并且该字段的其余部分或多或少相同。如果我包含所有内容会有所帮助吗?我只是怕会太长,因为只是属性和一个构造函数的重复

标签: vb.net linq


【解决方案1】:

您已将 Quantity 属性声明为 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的value参数中是否包含类型,但是如果编译器抱怨你可以删除它

【讨论】:

  • 天哪,我犯了这么一个基本错误。谢谢您的帮助!顺便说一句,您在设置器的值参数中包含类型是正确的
猜你喜欢
  • 2010-10-03
  • 2012-04-27
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
相关资源
最近更新 更多