// this function working perfectly
public dynamic CountTable()
{
return (from t in db.Users
group t by t.Type into g
select new
{
type = g.Key,
count = g.Count(),
ActiveGroups = (from t in g group t by t.Active into ag select new { active = ag.Key, count = ag.Count() })
}).ToList();
}
// and this loop working in MVC Controller
foreach (dynamic uct in ur.CountTable())
{
int x = uct.count;
}
But not working in template:
Line 12: @foreach (dynamic uct in ViewBag.ur.CountTable())
Line 13: {
Line 14: int adet = uct.count;
Line 15: }
Line 14: ‘object’ does not contain a definition for ‘count’
Why? What can I do?
Anonymous types are compiled into internal classes.
The standard binder used by
dynamicwill only bind to public members of public classes.Therefore, you cannot use it with anonymous types from a different assembly.
For more information, see here.