Companions of Stack Overflow in Spanish, I have the following error when doing this conditional ViewBag.p.Where(n => n.Lider == namelider
in the loop foreach
.
Here is part of my code:
<table class="table table-bordered">
@foreach (var item in ViewBag.c)
{
var namelider = item.lider;
<tr>
<td>@item.nombres</td>
<td>@item.lider</td>
<td>@item.eje_funcional</td>
@foreach(var peso in ViewBag.p.Where(n => n.Lider == namelider))
{
<td>@peso.Lider</td>
}
</tr>
}
</table>
The error that appears to me is the following:
You cannot use a lambda expression as the argument of a dynamically dispatched operation without first converting it to an expression tree or delegate type.
I add controller queries:
ViewBag.c = (from p in db.Collaborators
where p.grupo_lider == "NO"
select p).ToList();
ViewBag.p = (from p in db.Objectives
select p).ToList();
ViewBag.cal = (from p in db.CalificarColaboradors
select p).ToList();
What happens is that it
ViewBag
is a dynamic object (dynamic
) that does not inherit properties or methods of the object that you originally use, you need to make ancast
explicit to the original object type. This is called unboxing and it's pretty straightforward:What you could do is work as a
IEnumerable
the value of yourViewBag.p
to a list of the type of the object you have stored: