Whit
Whit
Explore posts from servers
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
I would like to perform an average against a collection of TValue wherein I know TValue is a number because I've constrained it to INumber<TValue>. However, when trying to use it in the LINQ expression, it doesn't appear to know how to handle the arbitrary number type:
var avg = group.Average(v => v.Value);
var avg = group.Average(v => v.Value);
CS0029: Cannot implicitly convert type 'TValue' to 'long?'
Any guesses as to how I can handle this generically without writing a separate overload for every numeric type? Thank you!
22 replies