Foreach viewbag object - filter by ID ?

websss
websss used Ask the Experts™
on
I have the following html code

@foreach (var e in ViewBag.Enterprise)
    {

    <li class="configurator-enterprise-li" id="@e.Id">
        <span>@e.Name</span>

        <ul id="site-ul">
            @foreach (var s in ViewBag.Sites)
            {

                <li class="site-li" id="@s.SiteId">@s.SiteName</li>
			}
    ...

Open in new window


This works well with 1 enterprise

But when I add 2 enterprises to the results, it list like this

Ent1
  - site1
  - site2
  - site3
Ent2
  - site1
  - site2
  - site3

instead of
Ent1
  - site2
  - site3
Ent2
  - site1


How do adjust the code so it only shows the correct data

Inside SITE there is a EnterpriseId
i.e. s.EnterpriseId

So I would need a where clause or something similar in razor?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Test your restores, not your backups...
Top Expert 2016
Commented:
I think you should be able to do this with LINQ.  Can't test easily right now, but try one of these as the second loop.

           @foreach (var s in ViewBag.Sites(x => x.EnterpriseId == e.id))

or

           @foreach (var s in ViewBag.Sites(x => x.EnterpriseId == @e.id))


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial