views:

21

answers:

1

I need a way to dynamically populate this query... to avoid repeating the same query which I will have to do about 20 times

public decimal percentage_of_property(string property)
{
    var total = Routines().Where(r=>r.property==true).Count();
    return (decimal)100 * total / routines_total();
}

This obviously doesn't work... but I put it there so you can see what I'm trying to achieve...

Thanks in advance.

+1  A: 

Assuming Routine is the type you can avoid reflection and use functional programming like so:-

public decimal percentage_of_property(Func<Routine, bool> propertyTest)
{
    var total = Routines().Where(r => propertyTest(r)).Count();
    return (decimal)100 * total / routines_total();
}

use it like:-

percentage_of_property(r => r.propertyName)
Hightechrider
That's awesome.
NachoF