-1

我在 Parent -> Child -> Grandchild 关系中有三种数据类型,如下所示:

Mission -> Activity -> Project

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

我的代码从点击页面到从数据库中获取数据然后填充页面大约需要六秒钟。这太长了,无论如何都想优化我的代码。使用 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++;
    }
}
}

这是带有 jQ​​uery 的 .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 插件来执行此操作。

4

1 回答 1

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 运行事物的方式有关,如果我单步执行代码,它只会命中一次查询,因此仍然可以解决该部分。

希望这对某人有帮助!

于 2013-01-30T17:23:53.810 回答