优化填充嵌套手风琴

时间:2013-01-23 19:29:08

标签: c# jquery .net performance optimization

我在Parent中有三种数据类型 - >孩子 - >孙子关系如下:

Mission -> Activity -> Project

其中包含关系的父ID(即'Project'包含'Activity'ID)。我有一些代码用于生成jQuery嵌套手风琴设置,因此用户可以单击“任务”以查看相关的“活动”,然后可以单击“活动”以查看相关的“项目”。

我点击页面大约需要6秒才能从数据库中获取数据,然后填充页面。这太长了,无论如何都希望优化我的代码。使用miniprofiler(miniprofiler.com)我可以看到它通过大量重复调用对数据库进行131次调用但我确定原因。

非常感谢您给予我的任何帮助!

LINQ查询我用来获取所有数据并组织它:

public IEnumerable<MissionWithActivities> GetTierTree()
{
var q = from mission in _context.tMissions
        join activity in _context.tActivities on mission.id equals activity.missionId
        join project in _context.tDefaultEventTypes on activity.id equals project.activityId
        where !project.isRemoved && project.defaultCategoryId == 4
        orderby mission.id, activity.id, mission.name
        select new DefaultEventType(project.tierLevel.TryParseEnum<GanttType>(GanttType.Unknown), DefaultCategoryRepository.CreateFrom(project.tDefaultCategory))
        {
            AllowNumericSuffix = project.allowNumericSuffix,
            AttachMilestoneMoniker = project.attachMilestoneMoniker,
            Description = project.description,
            Id = project.id,
            IsReadOnly = project.isReadOnly,
            IsSticky = project.isSticky,
            Name = project.name,
            Sid = project.sid,
            Style = project.style.TryParseEnum<GanttElementStyle>(GanttElementStyle.Unknown),
            TimeStamp = project.createdDT,
            UpdatedTimeStamp = project.updatedDT,
            Activity = new Activity { Id = activity.id, Name = activity.name, Mission = new Mission { Id = mission.id, Name = mission.name } }
        };
var q2 = q.GroupBy(
    e => e.Activity.Mission.Id,
    (mid, events) => new MissionWithActivities
    {
        Mission = events.First().Activity.Mission,
        Activities = events.GroupBy(
            e => e.Activity.Id,
            (aid, events2) => new ActivityWithEvents
            {
                Activity = events2.First().Activity,
                Events = events2
            })
    });
return q2.ToList();

}

代码隐藏我用来初始填充数据主义者,然后填充嵌套的手风琴:

public void SetTierTree(IEnumerable<MissionWithActivities> tierList)
{
dlMission.DataSource = tierList;
dlMission.DataBind();
}
public void dlMission_ItemDataBound(Object sender, DataListItemEventArgs e)
{
DataListItem item = e.Item;
MissionWithActivities mwa = (MissionWithActivities)item.DataItem;
var dlActivity = (DataList)item.FindControl("dlActivity");
dlActivity.DataSource = mwa.Activities;
dlActivity.DataBind();
var i = 0;
foreach (var project in mwa.Activities)
{
    DataListItem pItem = dlActivity.Items[i];
    var lbCreateNewProject = (LinkButton)pItem.FindControl("lbCreateNewProject");
    lbCreateNewProject.CommandArgument = project.Activity.Id.ToString();
    var dlProject = (DataList)pItem.FindControl("dlProject");
    dlProject.DataSource = project.Events;
    dlProject.DataBind();
    i++;
    var j = 0;
    foreach (var data in project.Events)
    {
        DataListItem lblItem = dlProject.Items[j];
        var lbEditProject = (LinkButton)lblItem.FindControl("lbEditProject");
        var lbRemoveProject = (LinkButton)lblItem.FindControl("lbRemoveProject");
        lbEditProject.CommandArgument = data.Id.ToString();
        lbRemoveProject.CommandArgument = data.Id.ToString();
        j++;
    }
}
}

这是带有jQuery的.aspx页面(我没有删除.net,所以它不会运行)但我想包含你的细读代码:

$(document).ready(function () {
        $("html").addClass("js");
        $(".row").mouseover(function () { $(this).addClass("over"); }).mouseout(function () { $(this).removeClass("over"); });
        $('h5').click(function () { $(this).prev(".heading_add").toggle(); });
        $(".row:even").addClass("alt");
        $.fn.accordion.defaults.container = false;
        $(function () {
            $("#acc1").accordion({
                el: ".h",
                head: "h4, h5",
                next: "div",
                initShow: "none"
            });
            $("html").removeClass("js");
        });
    });
<div id="main">
        <ul id="acc1" class="accordion">
        <asp:DataList ID="dlMission" runat="server" style="width:600px;">
        <ItemTemplate>
            <li>
                <h4><%# Eval("Mission.Name") %></h4>
                <div class="inner">
                    <ul>
                    <asp:DataList ID="dlActivity" runat="server">
                    <ItemTemplate>
                        <li>
                          <asp:LinkButton ID="lbCreateNewProject" CausesValidation="false" CssClass="heading_add" runat="server" Text="[ + ] Add New Project Type" OnCommand="lbCreateNewProject_OnCommand" />
                          <h5><%# Eval("Activity.Name") %></h5>
                          <div class="inner">
                          <asp:DataList ID="dlProject" runat="server">
                          <ItemTemplate>
                            <div class="row">
                              <%# Eval("Name") %><div class="action_buttons"><asp:LinkButton ID="lbEditProject" CausesValidation="false" runat="server" Text="Edit" OnCommand="lbEditProject_OnCommand" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:LinkButton ID="lbRemoveProject" CausesValidation="false" runat="server" Text="Remove" OnCommand="lbRemoveProject_OnCommand" /></div><br />
                            </div>
                          </ItemTemplate>
                          </asp:DataList>
                          </div>
                        </li>
                    </ItemTemplate>
                    </asp:DataList>
                    </ul>
                </div>
            </li>
        </ItemTemplate>
        </asp:DataList>
        </ul>
    </div>

对于好奇的我正在使用jQuery.nestedAccordion.js插件来执行此操作。

1 个答案:

答案 0 :(得分:0)

我弄清楚我遇到的缓慢的主要原因与LINQ的工作方式有关。我的主要问题是在LINQ查询中:

DefaultCategoryRepository.CreateFrom(project.tDefaultCategory)

CreateFrom函数导致LINQ获取与DefaultCategory相关的所有数据,这些数据非常广泛。我不知道LINQ会抓住树中的所有关系并返回它是否被使用。您可以在此处阅读更多信息:

http://msdn.microsoft.com/en-us/library/bb738633(v=vs.100).aspx

MiniProfiler仍然说重复调用正在进行131次,但我认为这与.Net运行的方式有关,如果我单步执行代码,它只会查询一次,所以仍然可以解决这个问题。< / p>

希望这有助于某人!