开发者

Help with asp.net mvc reflection and properties

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 / routi开发者_如何学JAVAnes_total();
}

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

Thanks in advance.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜