我怎样才能缩短我的linq代码?

时间:2010-08-11 08:30:53

标签: c# .net visual-studio linq linq-to-sql

喜;我试着运行我的代码。我的程序运行得更慢了。我需要在GetAliSpReqs(),GetMaintData(); GetAccess .... GET(... 如何在代码下面编写更有效的代码。它们太慢也没用。例如,我尝试使用GetAliSpReqs()编写小于1-2行?我怎么能够 ?请帮我...

  public void LoadById(string SearchItem)
        {
            var myTechnicTasks = engTaskCtx.Tasks.Where(task => task.MyTechnicReference.StartsWith(SearchItem)).Select(task => new MyTask()
            {
                id = task.id,
                MyTechnicReference = task.MyTechnicReference,
                MPDReference = task.MPDReference,
                tasktypeid = task.tasktypeid,
                shortdesc = task.shortdesc,
                interval = task.interval,
                critical = task.critical,
                mandatory = task.mandatory,
                dupinsp = task.dupinsp,
                dualsystemmaint = task.dualsystemmaint,
                MPDSkill = task.MPDSkill,
                MPDCrew = task.MPDCrew,
                MPDAccessMH = task.MPDAccessMH,
                MPDTotalMH = task.MPDTotalMH,
                extratime = task.extratime,
                Team = task.Team,
                MaintData = EngGetCalculatedTaskField.GetMaintData(task.id),
                AliSpReqs = EngGetCalculatedTaskField.GetAliSpReqs(task.id),
                Access = EngGetCalculatedTaskField.GetAccess(task.id),
                preperation = task.preperation,
                longdesc = task.longdesc,
                applnotes = task.applnotes
            });
            MyTechnicTaskList = myTechnicTasks.ToList();
        }




    public static class EngGetCalculatedTaskField
    {
        private static TaskMaintenanceDataDataContext engTaskCtx { get; set; }
        public static string GetMaintData(int taskID)
        {
            try
            {
                using (TaskCardContext.TaskMaintenanceDataDataContext dc = new TaskCardContext.TaskMaintenanceDataDataContext())
                {
                    string maintenanceData = String.Empty;

                    foreach (var item in dc.TaskRelations.Where(tableRaletions => tableRaletions.TaskId == taskID && tableRaletions.RelTypeId == 12))
                    {
                        maintenanceData += item.RefMaintenance.shortdesc + "; ";
                    }

                    return maintenanceData.Substring(0, maintenanceData.Length - 2);
              }
            }
            catch
            {
                return String.Empty;
            }

        }
        public static string GetAliSpReqs(int taskID)
        {
            #region Old
            try
            {
                using (TaskCardContext.TaskMaintenanceDataDataContext dc = new            TaskCardContext.TaskMaintenanceDataDataContext())
                {
                    string aliSpReqs = String.Empty;

                    foreach (var item in dc.TaskRelations.Where(tableRaletions => tableRaletions.TaskId == taskID && tableRaletions.RelTypeId == 13))
                    {
                        aliSpReqs += item.RefAliSpReq.shortdesc + "; ";
                    }
                    return aliSpReqs.Substring(0, aliSpReqs.Length - 2);


                }
            }
            catch
            {
                return String.Empty;
            } 
            #endregion


        }
        public static string GetAccess(int taskID)
        {
            #region Old
            try
            {
                using (TaskCardContext.TaskMaintenanceDataDataContext dc = new TaskCardContext.TaskMaintenanceDataDataContext())
                {
                    string access = String.Empty;

                    foreach (var item in dc.TaskRelations.Where(tableRaletions => tableRaletions.TaskId == taskID && tableRaletions.RelTypeId == 15))
                    {
                        access += item.RefAccessPanel.shortdesc + "; ";
                    }
                    return access.Substring(0, access.Length - 2);


                }
            }
            catch
            {
                return String.Empty;
            } 
            #endregion

        }
}

2 个答案:

答案 0 :(得分:5)

我们以这段代码为例:

string aliSpReqs = String.Empty;

foreach (var item in dc.TaskRelations.Where(tableRaletions => 
                  tableRaletions.TaskId == taskID 
                  && tableRaletions.RelTypeId == 13))
{
    aliSpReqs += item.RefAliSpReq.shortdesc + "; ";
}
return aliSpReqs.Substring(0, aliSpReqs.Length - 2);

你在循环中连接字符串。这是个坏主意。相反,试试这个(假设是.NET 4):

var query = c.TaskRelations.Where(r => r.TaskId == taskID 
                                       && r.RelTypeId == 13))
                           .Select(r => r.RefAliSpReq.shortdesc);
return string.Join("; ", query);

在.NET 3.5中,您需要使用它:

var query = c.TaskRelations.Where(r => r.TaskId == taskID 
                                       && r.RelTypeId == 13))
                           .Select(r => r.RefAliSpReq.shortdesc);
return string.Join("; ", query.ToArray());

不可否认,我无法分辨是否实际是什么让这个变慢或者没有 - 但很可能是,如果有很多字符串。

顺便说一句,这是一个可怕的想法:

catch
{
    return String.Empty;
}

捕获特定的异常 - 或者在大多数情况下,只是让异常传播给调用者。在至少,你应该记录异常,这样你就知道出了什么问题。

答案 1 :(得分:1)

您的所有函数都使用不同的参数调用相同的代码:

try
{
    using (TaskCardContext.TaskMaintenanceDataDataContext dc = new TaskCardContext.TaskMaintenanceDataDataContext())
    {
        string aliSpReqs = String.Empty;

        foreach (var item in dc.TaskRelations.Where(tableRaletions => tableRaletions.TaskId == taskID && tableRaletions.RelTypeId == 13))
        {
            aliSpReqs += item.RefAliSpReq.shortdesc + "; ";
        }
        return aliSpReqs.Substring(0, aliSpReqs.Length - 2);
    }
}
catch
{
    return String.Empty;
}

所以让我们用它来创建一个函数:

private static string GetData(int taskID, int typeID)
{
    try
    {
        using (TaskCardContext.TaskMaintenanceDataDataContext dc = new TaskCardContext.TaskMaintenanceDataDataContext())
        {
            //This was taken from Jons answer!!
            var query = c.TaskRelations.Where(r => r.TaskId == taskID 
                                              && r.RelTypeId == typeID))
                                       .Select(r => r.RefAliSpReq.shortdesc);
            return string.Join("; ", query.ToArray());
        }
    }
    catch
    {
        return String.Empty;
    }
}

现在您可以从所有其他功能调用此功能,例如:

public static string GetMaintData(int taskID)
{
    return GetData(taskID, 12);
}

这样可以缩短您的代码。为了提高性能,你应该接受Jons的回答并最后使用+=来连接你的字符串,而不是每一步,因为这是非常糟糕的。