views:

36

answers:

2

Hi, I have two tables Category 1..* Advertisement.

I want to get the advertisements Count for each category. To do so, I use that query:

var catList = (from c in DB.Category.Include("Advertisement")
                  select new { c.Name, c.Advertisement.Count }
                  ).ToList();

How to access the e.g the first element's property Name and Count via View ?

A: 

You will have to use a collection of a strongly typed object instead of using anonymous types.

Please see http://stackoverflow.com/questions/1178984/dynamic-typed-viewpage for an explanation, but the readers' digest version is that anonymous types are internal.

Josh
A: 

I'd use a view model, like:

var catList = (from c in DB.Category.Include("Advertisement")
                  select new CategorySummaryViewModel{ Name = c.Name, AdsCount = c.Advertisement.Count }
                  ).ToList();

Unrelated & not sure: but I think you can take out the .Include in that linq query / since you are getting a projection, not a Category instance that you want it's Advertisement property populated.

eglasius