简体   繁体   中英

How can i shorter my linq codes?

hi; i try to run my codes. My program more slowly runnig. i need to give performance also write less codes in GetAliSpReqs(), GetMaintData();GetAccess....GET(... How can i write more effective below codes. They are too slow also not useful. forexample i try to write les than 1-2 line with GetAliSpReqs()? How can i ? please help me...

  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

        }
}

Let's take this bit of code as an example:

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);

You're concatenating strings in a loop. That's a bad idea. Instead, try this (assuming .NET 4):

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

In .NET 3.5 you'd need to use this instead:

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

Admittedly I can't tell whether that's actually what's making this slow or not - but it could well be, if there are a lot of strings.

As an aside, this is a terrible idea:

catch
{
    return String.Empty;
}

Catch specific exceptions instead - or in most cases, just let the exception propagate to the caller. At the very least you should log the exception so you know what's going wrong.

All of your functions are calling the same piece of code with a different parameter:

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;
}

So lets make a function out of it:

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;
    }
}

Now you can call this function from all your other functions like:

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

So this makes your code much shorter. For a performance boost you should take Jons answer and do the concatenation of your string at last and not at every step by using += , cause this is very bad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM