帮助asp.net mvc反射和属性

时间:2021-02-09 03:27:12

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

我需要一种方法来动态填充这个查询……为了避免重复相同的查询,我需要重复20次

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 个解决方案

#1


2  

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)

#1


2  

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)