I'm working on an application in which it takes quite a bit of time to initialize the data.
Some background: I'm creating a sort of pivot table in which I turn Figure 1 below into Figure 2. I've gone through with Stopwatchs and have been able to isolate a single line of code that takes 2 seconds during program load (it is in a loop called for roughly 2-300 rows of data, 2 seconds is the total time across all iterations). That line is marked in the code example below, Figure 3.
My question is if there is a better way to structure the example method such that I can avoid whatever overhead that line is causing. It is worth noting that when that line executes, mChar contains sometimes 0 elements, but up to around 5 or 1 (see edit below), so I can't imagine what would cause this to take so much time to execute.
Figure 1
SAMPLENUMBER | ANALYSIS_NAME | ANALYSIS_STATUS ---------------------------------------------- 1234 | NO3 | I 1234 | SO4 | C 5678 | NO3 | C Figure 2
SAMPLENUMBER | NO3 | SO4 ---------------------------------------------- 1234 | I | C 5678 | C | Figure 3
private List<string> GetPivotData(DataRow sourceRow, DataTable sourceTable) { int pivotStartIndex = sourceTable.Columns.IndexOf(Cols.HotDate) + 1; var retList = new List<string>(sourceTable.Columns.Count - pivotStartIndex); for (int i = pivotStartIndex; i < sourceTable.Columns.Count; i++) { string[] elementTest = sourceTable.Columns[i].ColumnName.Split('-'); var mChars = from s in mainData.AsEnumerable() where s.Field<string>(Cols.Samplenumber) == sourceRow.Field<string>(Cols.Samplenumber) && s.Field<string>(Cols.ElementName) == elementTest[0].Trim() && s.Field<string>(Cols.AnalysisNumber) == elementTest[1].Trim() select s.Field<string>(Cols.AnalysisStatus); retList.Add(mChars.FirstOrDefault()); // <-- this is the line taking so much time. } return retList; } I've tried things like having retList as an array rather than a list, as well as calling .ToList() on my LINQ result and using .Find(x => true) and they really haven't had any effect. The method in Figure 3 is itself inside a for loop, called roughly 200 times, once per sample. The for loop inside the method itself iterates on average 5 times, so that line of code gets called about 1000 times, depending on the data. That means that the average time for the line is around 2ms, which may just be too small in itself to be able to do anything with. Swapping my for loops for Parallel.For halves the time it takes, but I've found that my logic doesn't work well with concurrency. At all.
Edit: I should clarify why I'm using FirstOrDefault() I suppose and I need to clarify an error in my initial post as well. The error is that I said mChars can contain up to 5 elements when FirstOrDefault() is called (it is retList that has up to 5). It will only ever have 0 or 1 element(s). When it's empty, I still need to add a null to the list, so I'm looking into a way to add the mChars result directly to the list without any searches at all.