score:0

the following is the answer that works for me:

``````public double averageif<t1,t2>(ienumerable<t1> average_range, ienumerable<t2> criteria_range, func<t2, bool> criteria)
{
var t1andt2 = average_range.zip(criteria_range, (l, r) => tuple.create<t1, t2>(l, r));
return t1andt2.where(x => criteria(x.item2)).average(x => convert.todouble(x.item1));
}

void main()
{
var average_range = new list<string>() {"10.1", "10.1", "0", "0"} ;
var criteria_range = new list<bool>() {true, true, false, false };
func<bool, bool> criteria = c => c == true;
averageif(average_range, criteria_range, criteria).dump();
}
``````

score:16

i'd like to recreate a generic version of the excel function averageif

why don't you just use linq?

``````var average = collection.where(x => x.something)
.average(x => x.someproperty);
``````

note that this will throw an exception if there are no matching elements. if you don't want that, you could use:

``````var average = collection.where(x => x.something)
.defaultifempty()
.average(x => x.someproperty);
``````

it's not clear why you would want to create a separate method for this.