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>
@moooodie
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
public static double Average<TValue>(this IEnumerable<TValue> values)
where TValue : INumber<TValue>
{
double sum = 0;
var count = 0;

foreach (var value in values.ToList())
{
sum += Convert.ToDouble(value);
count++;
}

return sum / count;
}
public static double Average<TValue>(this IEnumerable<TValue> values)
where TValue : INumber<TValue>
{
double sum = 0;
var count = 0;

foreach (var value in values.ToList())
{
sum += Convert.ToDouble(value);
count++;
}

return sum / count;
}
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
You're right - makes sense to return the average as a double
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
Working on it
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
I'll give that a whirl - thanks for the suggestion
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
That might be blasphemy.. but also might be what's called for
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
It's the only way not giving me squigglies
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
I'd like to, but I'm not seeing much in the way of alternatives
22 replies
CC#
Created by Whit on 8/6/2023 in #help
How to perform LINQ Average against collection of TValue constrained as INumber<TValue>
Oddly, if I simply cast v.Value as dynamic, VS seems to think I'm casting to an int and returns a double and that's not right - as it might be a decimal that was provided.
22 replies