Hi, I need to calculate a sum of product of two fields. This will be used for average weighted price. avgPrice = sum( price*volume) / sum(volume). Both price1 and price2 return error "Specified cast is not valid."
var result3 = from sym in dataTableAsEnumerable()
group sym by new { symbol = sym["symbol"] } into grouped
select new
{
// SYMBOL = sym.Field<string>("symbolCAN"),
SYMBOL = grouped.Key.symbol,
tradeTimeMin = grouped.Min(e => e["tradeTimeMin"]),
tradeTimeMax = grouped.Max(e => e["tradeTimeMax"]),
volume = grouped.Sum(e => (int)e["volume"] ),
price1 = grouped.Sum(e => (double)e["volume"] * (double)e["symbol"]) / grouped.Sum(e => (double)e["volume"]),
price2 = grouped.Sum(e => ( e.Field<decimal>("volume") * e.Field<decimal>("symbol")))
};