I have problem with performance and don't understand behaviour of EF. I use ASP.NET MVC application and have the following code in model:

    public List<Portal> PortalListWithCategories()
    {
        List<Portal> q = new List<Portal>();
            q = (from i in _dataContext.Portals.Include("Categories").Include("Ideas") where i.Categories.Count > 0 orderby i.DefaultPortal descending select i).ToList();
        return q;
    }

I call it from controller:

            portalList = _repository.PortalListWithCategories();

as I understand, EF should execute batch request and return collection of portals with nested collections "Categories" and "Ideas".

But on view I have following:

                    @foreach (var category in portal.Categories.Where(n => n.Ideas.Count > 0 && n.Portals.Any(g => g.PortalID == portal.PortalID)))
                    {
                        if ((from e in category.Ideas where e.Portals.Any(t => t.PortalID == portal.PortalID) select e).Count() > 0)
                        {
                            string categoryLink = Url.RouteUrl("Full", new { PortalID = portal.PortalID, CategoryID = category.CategoryID, action = "Ideas" });
                            List<NEOGOV_Ideas.Models.Ideas> ideas = category.Ideas.Where(o => o.Portals.Any(p => p.PortalID == portal.PortalID) && o.Categories.Any(h => h.CategoryID == category.CategoryID)).OrderByDescending(k => k.CreatedDateTime).ToList();
                        <div class="grid_4">
                            <h4>
                                <a href="@categoryLink">@category.CategoryName<span class="count_link">&nbsp;(@ideas.Count())</span>
                                    <span class="follow_link">&raquo;</span></a></h4>
                            <ul>
                                @foreach (var idea in ideas.Take(3))
                                {
                                    string ideaLink = Url.RouteUrl("IdeaShort", new { id = idea.IdeaID });
                                    if (!idea.IdeaTypeReference.IsLoaded) { idea.IdeaTypeReference.Load(); }
                                    string cssclass = " class=\"" + idea.IdeaType.TypeName.ToLower() + "\"";
                                    <li><a href="@ideaLink" @cssclass>@idea.Title</a></li>
                                }
                            </ul>
                        </div>
                                if (i == 2)
                                {
                        <div class="clear">
                        </div>
                                }
                                i++;
                        }
                    }

as I understand, I should not have new requests to DB, but I have and very many. Why?

link|improve this question
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown

Browse other questions tagged or ask your own question.