Net mvc 3 foreach in foreach?

I'm not familiar with EF, but I know in LINQ-to-SQL, you could use DataLoadOptions to include other joined data: HelpMeContext db = new HelpMeContext(); DataLoadOptions options = new DataLoadOptions(); options. LoadWith(c => c. SubCategories); options.

LoadWith(sc => sc. SubSubCategories); db. LoadOptions = options; // now this should load subcategories and sub-subcategories with it.

Var model = db.Category.ToList() You could also try selecting all of the data into an anonymous type, but it's a little messier to read and to work with as well: var model = db. Category . Select(c => new { Category = c, SubCategories = c.SubCategories.

Select(sc => new { SubCategory = sc, SubSubCategories = sc. SubSubCategories } )} ).ToList() From which you could say: foreach (var item in Model) { //item.Category. Name @foreach (var sc in item.

SubCategories) { //sc.SubCategory. Name @foreach (var ssc in sc. SubSubCategories) /ssc.Name } } }.

I'm not familiar with EF, but I know in LINQ-to-SQL, you could use DataLoadOptions to include other joined data: HelpMeContext db = new HelpMeContext(); DataLoadOptions options = new DataLoadOptions(); options. LoadWith(c => c. SubCategories); options.

LoadWith(sc => sc. SubSubCategories); db. LoadOptions = options; // now this should load subcategories and sub-subcategories with it.

Var model = db.Category.ToList(); You could also try selecting all of the data into an anonymous type, but it's a little messier to read and to work with as well: var model = db. Category . Select(c => new { Category = c, SubCategories = c.SubCategories.

Select(sc => new { SubCategory = sc, SubSubCategories = sc. SubSubCategories } )} ).ToList(); From which you could say: @foreach (var item in Model) { //item.Category. Name @foreach (var sc in item.

SubCategories) { //sc.SubCategory. Name @foreach (var ssc in sc. SubSubCategories) /ssc.Name } } }.

Your idea is good, but the Linq-to-sql code is messing up in EF. The corrisponding function to DataLoadOptions should be .Include() .. but it doesn't seem to work for some reason. – Niels Robin Aagaard Jun 21 at 3:59 FYI, the crazy error msg I get is: The model item passed into the dictionary is of type 'System.Collections.Generic.

List`1f__AnonymousType2`2helpme.mvc.Models. Catego? Ry,System.Collections.Generic.

IEnumerable`1f__AnonymousType1`2helpme.mvc. Model?s. SubCategory,System.Collections.Generic.

ICollection`1helpme.mvc.Models. SubSubCa? Tegory', but this dictionary requires a model item of type 'System.Collections.Generic.

IEnumerable`1helpme.mvc.Models. Category'. – Niels Robin Aagaard Jun 21 at 4:00 @Niels: Ah, yeah for the second case you might not be able to use a strongly-typed model, because it involves custom anonymous types.

– mellamokb Jun 21 at 13:00 @Niels: Regarding the "doesn't seem to work for some reason," do you just mean it still yields empty collections, or does it generate some sort of exception? – mellamokb Jun 21 at 13:01 1 model. SubCategories is populated when I insert a break point in the controller, but when I get to the view to seems to have lost it?

It only displays my Category level and not SubCategory level even though I foreach them both. – Niels Robin Aagaard Jun 21 at 14:03.

Foreach (var sc in item. @foreach (var ssc in sc.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions