2016-07-05 3 views
0

私は以下で定義したカスタムクラスAggregate_TableのIQueryableでLINQを使用してvb.netで合計を実行しようとしています。私は通常の合計関数は、私のカスタムクラスのIQueryable(Of T)に適用すると、構文エラーを与えるようにこれを達成する方法がわかりません。カスタムクラスIQueryable(Of T)の集計を実行する

私は次のクエリで開始:DataContextのオブジェクト・データベース内のテーブル_58k_SIsから単純なクエリである

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) 

を。このクエリ結果の型は匿名データ型であるため、特定のテーブル型を取り込む関数を作成する必要がありますので、上記のクエリ結果と同じ属性でクラス名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オブジェクトのリストを作成し、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

Aggregated_SIクラスの完全な定義を含めてください。フィールド宣言(プライベート)のみが含まれていますが、明らかにQuantityプロパティを使用しており、その定義を表示していません。エラーから、私は、Quantityプロパティが整数の代わりにオブジェクトを返すと仮定します。 –

+0

私はquantityプロパティを含んでおり、残りのフィールドは多かれ少なかれ同じです。すべてを含めると助けになるのだろうか?私はちょうどプロパティと1つのコンストラクタの繰り返しですので、それはあまりにも長くなることを恐れている – chrishendra93

答えて

1

Public Property Quantity As Integerではなく、Public Property Quantityという数量プロパティを宣言しました。これにより、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を書いたので、それは長い時間がかかった、私はセッターの値のパラメータでタイプを含めて約わからないが、あなたはそれを削除することができますコンパイラが文句を言う場合

+0

ああ私は、このような基本的な間違いをした。助けてくれてありがとう!ところで、setterのvalueパラメータに型を含めるのは正しいです – chrishendra93

関連する問題