我们可以添加LINQwhere方法参数的条件吗
c#
以下是我在某些条件下使用的现有方法
public List<MenuItem> getTopMenu(List<Role> Id)
{
// here many lines code for checking id value
}
我正在更改上述方法以检查方法参数中的条件如下所示但不工作的代码
public List<MenuItem> getTopMenu(List<Role> Id.Where(r=>r.status=="Active"))
{
}
错误来了,我只需要在方法中传递活动 id 我不能单独写入每个方法。
回答
在方法内移动过滤器:
public List<MenuItem> getTopMenu(List<PersonRoleDTO> Id)
{
Id = Id.Where(r=>r.status=="Active").ToList();
// Whatever else
}
您不能将语句作为方法的参数。