Optimizing FirstOrDefault 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&lt;string&gt; GetPivotData(DataRow sourceRow, DataTable sourceTable)
{
  int pivotStartIndex = sourceTable.Columns.IndexOf(Cols.HotDate) + 1;
  var retList = new List&lt;string&gt;(sourceTable.Columns.Count - pivotStartIndex);
  for (int i = pivotStartIndex; i &lt; sourceTable.Columns.Count; i++)
  {
    string[] elementTest = sourceTable.Columns[i].ColumnName.Split('-');
    var mChars = 
        from s in mainData.AsEnumerable()
        where s.Field&lt;string&gt;(Cols.Samplenumber) == sourceRow.Field&lt;string&gt;(Cols.Samplenumber)
          &amp;&amp; s.Field&lt;string&gt;(Cols.ElementName) == elementTest[0].Trim()
          &amp;&amp; s.Field&lt;string&gt;(Cols.AnalysisNumber) == elementTest[1].Trim()
        select s.Field&lt;string&gt;(Cols.AnalysisStatus);
    retList.Add(mChars.FirstOrDefault()); // &lt;-- 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 =&gt; 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.
The line with from only defines the enumeration, but it doesn't actually run over the list and apply your select clause. In your case, that one is only applied when you call FirstOrDefault.

Therefore, to speed up the call to FirstOrDefault you need to speed up your where condition. I mean this one:

s.Field&lt;string&gt;(Cols.Samplenumber) == sourceRow.Field&lt;string&gt;(Cols.Samplenumber)
      &amp;&amp; s.Field&lt;string&gt;(Cols.ElementName) == elementTest[0].Trim()
      &amp;&amp; s.Field&lt;string&gt;(Cols.AnalysisNumber) == elementTest[1].Trim()


The most expensive parts about this are probably the calls to s.Field&lt;string&gt;(...), followed by the Trim() calls. All of these can be cached. Just convert the maindata into something that's cheap to access, and only do the conversion once, outside of the for loop. Something more or less like this (this won't run as is, only use it as inspiration):

struct TempItem
{
    public string Samplenumber;
    public string ElementName;
    public string AnalysisNumber;
    public string AnalysisStatus;
}
string sourceSampleNumber = sourceRow.Field&lt;string&gt;(Cols.Samplenumber);
var lookUpList = mainData.AsEnumerable().Where(i =&gt; i.Samplenumber == sourceSampleNumber).Select(i=&gt; new TempItem {
        Samplenumber = s.Field&lt;string&gt;(Cols.Samplenumber),
        ElementName = s.Field&lt;string&gt;(Cols.ElementName),
        AnalysisNumber = s.Field&lt;string&gt;(Cols.AnalysisNumber),
        AnalysisStatus = s.Field&lt;string&gt;(Cols.AnalysisStatus)
    }).ToList(); // the ToList() part here forces application of the linq expressions

for (int i = pivotStartIndex; i &lt; sourceTable.Columns.Count; i++)
{
    string[] elementTest = sourceTable.Columns[i].ColumnName.Split('-');
    string elementName = elementTest[0].Trim();
    string analysisNumber = elementTest[1].Trim();
    var mChars = lookUpList.Where(i =&gt; i.ElementName == elementName &amp;&amp; i.AnalysisNumber == analysisNumber).Select(i=&gt;i.AnalysisStatus);
    retList.Add(mChars.FirstOrDefault()); 
}