Asp.net mvc reflection and properties reference

I need a way to dynamically populate this request ... to avoid repeating the same request that I will need 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();
}

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

Thanks in advance.

+3
source share
1 answer

Assuming Routineyou can avoid reflection and use functional programming as follows: -

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)
+2
source

Source: https://habr.com/ru/post/1762524/


All Articles