© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
21 replies
Whit

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!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Put results of LINQ query into collection
C#CC# / help
3y ago
How to perform nested one-to-many join in LINQ
C#CC# / help
2y ago
Concurrent collection type similar to Dictionary<TKey, List<TValue>>
C#CC# / help
7mo ago
✅ Returning (Collection) from LINQ Query
C#CC# / help
3y ago