views:

113

answers:

1

I have an ASP.NET MVC app in which I am iterating through a Linq result set. Each row in the result set contains a property which is an EntitySet itself. When I try to iterate through the inner result set, I get an error message: "Invalid object name EntitySetOfSubItem" when trying to load the page. How do I process this collection?

<% foreach item in Model { %>
    ... code
    <% foreach subitem in item.EntitySetOfSubItems { %>
+1  A: 

You would do it something like this:

<% foreach(YourType item in Model) { %>
    ... code
    <% foreach(OtherType subitem in item.EntitySetOfSubItems) { %>

By typing the iteration variable, you inform the compiler what attributes are available on the subitem.

John Gietzen
I tried that and am now getting a compilation error stating that "The type or namespace name 'OtherType' could not be found (are you missing a directive or an assembly reference?)"
John Prideaux
Ok -- gave fully qualified AssemblyName.Models.OtherType and now am back to the original error message. foreach (AssemblyName.Models.OtherType subitem ...)
John Prideaux
Well, if that is the case, your "OtherType" probably doesn't actually have that property. Could you "navigate to" the "OtherType" and verify that there is actually an "EntitySetOfSubItems" property?
John Gietzen
In my Model, the property is definitely of type EntitySet<OtherType>. When I hover over item.EntitySetOfSubItems in the view page, it states the same thing.
John Prideaux
I think I have a deeper problem. Let me check on something else.
John Prideaux
My apologies. I have an error occurring in my db code that is not giving an exception so I do not have an item of OtherType in YourType. I'm going to accept your answer because it appears to be correct though not the solution to my real problem.
John Prideaux