Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force the Average function to include null values?

Tags:

c#

I wrote the following inside my asp.net mvc application:

double d = visits.Average(d=> d.amount);

But the above code ingores any object that is Null, returning an unrealistic result in my case. How can I force the Average function to calculate the average as :

double d = visits.Sum(d=> d.amount)/visit.Count();

1 Answers

So you want null values to be interpreted as 0?

double d = visits.Average(d => d.amount ?? 0);
like image 146
univerio Avatar answered Aug 30 '26 13:08

univerio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!