I have a table like this:
PRO_ID CATEGORY FINALLY_OK
200 1 55
200 1 60
200 2 65
200 2 80
250 1 55
250 1 60
250 2 65
250 2 80
Here is my code :
CType(grdQuality.DataSource, DataView).Table.Rows.Cast(Of DataRow)
.Select(Function(item) New With {.Ok = CType(item.Item("FINALLY_OK"), Decimal), .PRO_ID = item.Item("PRO_ID"), .Category = item.Item("CATEGORY")})
.GroupBy(Function(item) New With {.PRO_ID = item.PRO_ID, .Category = item.Category})
.Select(Function(item) New With {item.Key.PRO_ID, item.Key.Category, .AVG = item.Average(Function(i) i.Ok)})
but this returns all columns again and group by
does not work properly
is there anything wrong with my code?
You're using an anonymous type to group, but you're not making any of those properties key properties... which means they aren't used in the equality tests. Try this:
.GroupBy(Function(item) New With { Key .PRO_ID = item.PRO_ID, Key .Category = item.Category})
(You won't see that in C# samples, as all properties in C# anonymous types are implicitly key properties.)
See more on this question at Stackoverflow