0

私は次のテーブル構造を持っています:

tempDt

col1 = Product (Integer)  
col2 = Customer (Integer)  
col3 = Center (Integer)  
col4 = Date (Date)  
col5 = Price (Double)  
col6 = Sales (Double)  

この例の行:

1;1;1;01.01.2012;4.39;20000  
1;1;1;02.02.2012;4.46;15000  
1;1;1;03.03.2012;4.22;25000  

そして、次の Linq クエリ:

For Each item In From u In tempDT.AsEnumerable
Group u By Product = u("Product"), Customer = u("Customer"), Center = u("Center") Into Group
Select Product, Customer, Center, Price = Group.Average(Function(g) g("Price")), Sales = Math.Round(Group.Sum(Function(g) CDbl(g("Sales"))), 2)

tmpDt.Rows.Add(item.Product, item.Customer, item.Center, item.Price, item.Sales)
Next

しかし、結果として次の行が得られます: 1;1;1;4.0;60000

Group.Average で "Price" 列が切り捨てられます。何が問題なのですか?

4

2 に答える 2

0

あなたのクエリはかなり判読できません。私の変更を見てください(例えば、強力な型DataRow.Field拡張メソッドを使用して):

Dim groups = From u In tempDT.AsEnumerable
             Group u By Key = New With {
                 .Product = u.Field(Of Integer)("Product"),
                 .Customer = u.Field(Of Integer)("Customer"),
                 .Center = u.Field(Of Integer)("Center")
             } Into PCCroup = Group
             Select New With {
                 .PCC = Key,
                 .Price = PCCroup.Average(Function(u) u.Field(Of Double)("Price")),
                 .Sales = Math.Round(PCCroup.Average(Function(u) u.Field(Of Double)("Sales")), 2)
             }

For Each item In groups
    tmpDt.Rows.Add(item.PCC.Product, item.PCC.Customer, item.PCC.Center, item.Price, item.Sales)
Next
于 2012-06-04T07:43:54.837 に答える
0

コンパイル時にタイプがわからない場合、どのオーバーロードが選択されるかは明確ではありません。あなたは試すことができます:

Group.Average(Function(g) g.Field(Of Double)("Price"))

それでも問題が解決しない場合は、LINQ クエリを実行する前に、値が実際に切り捨てられていないことを再確認する必要があります (たとえば、データ テーブルに入力するときなど)。

于 2012-06-04T07:13:03.247 に答える