title: Take result with max count using LINQ
body: <p>I have a data table in memory that looks something like this:</p>
  
  <pre><code>Tape               Mask                Count
  BT1                DML69               3452
  BT2                DML69               1569
  BT2                DML87               2745
  BT3                DML69               3215
  BT3                DML87               1542
  BT4                DML87               3214
  BT5                DML69               2132
  BT5                DML87               1241
  </code></pre>
  
  <p>I need a LINQ query that will return a data row for each tape, one result for each tape, including only the mask with the maximum count. i.e.  </p>
  
  <pre><code>Tape               Mask                Count
  BT1                DML69               3452
  BT2                DML87               2745
  BT3                DML69               3215
  BT4                DML87               3214
  BT5                DML69               2132
  </code></pre>
  
  <p>I have tried doing this several different ways, all with no success.  Here is my latest attempt:</p>
  
  <pre><code>        foreach (string tape in singOne.GetDistinctTapes(
                                      converter.ConvertProcessesIDToSingOneID(selectedWafer)))
          {
              var tapeMaskQuery =
                  from row in tempTable.AsEnumerable()
                  where row.Field&lt;string&gt;("location1") == tape
                  select row;
  
              if (tapeMaskQuery.Count() == 1)
              {
                  tapeMaskCountTable.Rows.Add(tapeMaskQuery.ElementAt(0));
              }
              else
              {
                  var maxMaskQuery =
                      (from fields in tapeMaskQuery.AsEnumerable()
                      select new
                      {
                          tape = fields.Field&lt;string&gt;("location1"),
                          mask = fields.Field&lt;string&gt;("mask"),
                          count = fields.Field&lt;Int64&gt;("count(*)")
                      }).Max(x =&gt; x.count);
                  tapeMaskCountTable.Rows.Add(maxMaskQuery);
              }
          }
  </code></pre>
  
  <p>Here the singOne.GetDistinctTapes() simply returns a list of distinct tapes on the wafer.  tempTable is a DataTable that is in the form of the first table in this post.  I check to see if the there are more than one result for each tape; if there is, then I run the "maxMaskQuery", which only seems to return a DataRow including just the count column, not the tape and mask column.  I need all three columns to be present in the end, just like in the second table in this post.</p>
  
  <p>Any help on how to implement this would be greatly appreciated.</p>
  
  <p>Regards,</p>
  
  <p>Kyle</p>