2012-06-04 25 views
0

我有如下表结构:的LinQ Group.Average截断我的价值观

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 

但是结果我ge t以下行: 1; 1; 1; 4.0; 60000

Group.Average截断“价格”列,出了什么问题?

回答

0

我不清楚哪一个过载会被选中,因为它不知道编译时的类型。你可以尝试:

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

如果没有帮助,你应该仔细检查该值真的不是截断你的LINQ查询,例如同时填写数据表。

+0

谢谢,这解决了我的问题。 – user1434532

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