0

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 ?

Tony
  • 12,405
  • 36
  • 126
  • 226

2 Answers2

0

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

Please see Dynamic typed ViewPage for an explanation, but the readers' digest version is that anonymous types are internal.

Community
  • 1
  • 1
Josh
  • 16,286
  • 25
  • 113
  • 158
0

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
  • 35,831
  • 5
  • 65
  • 110