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:
You’re concatenating strings in a loop. That’s a bad idea. Instead, try this (assuming .NET 4):
In .NET 3.5 you’d need to use this instead:
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 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.