title: Find duplicates in datatable with multiple columns except two
body: <p>I am new at coding and trying to check a spreadsheet for duplicate rows. The spreadsheet has 50 columns and every column has to be compared except two. If the rows is duplicated, it will combined them to one row and the amounts in columns REQNUM and AUTHNUM will be summed. Most of the samples I found use "Field("a column name")". Because of the large amount of columns, I want to use a variable that excluded the two I don't need in compare.</p>
  
  <p>Example:<br/>
  Before. The dots represent more columns<br/>
  COL1|COL2|COL3|...|REQNUM|AUTHNUM <br/>
  :-----: | :-----: | :----: |...| :----------: | :-----------: |....<br/>
    x | y  | z  |...|  1   |  1<br/>
    x | y  | z  |...|  2   |  3<br/></p>
  
  <p>After <br/>
  COL1|COL2|COL3|...|REQNUM|AUTHNUM <br/>
  ------- | ------ | ------ | ...|------------ | ------------|.... <br/>
    x | y  | z  |...|  3   |  4</p>
  
  <p>This is the code I have and it seems close but not quite right. I was expecting a result of just duplicate rows, so later I can run it through a foreach that will sum and delete extra rows. dtrow gets me the columns I want.(Thanks to <a href="https://stackoverflow.com/questions/34454745/linq-excluding-a-column">Linq Excluding a column</a>). When I try to use this the variable in my query, I get no results and if I remove the "g.Count() > 1" I get all the rows with them missing the two columns. I would like to keep the all the two columns in the results and not have to add them back in later. </p>
  
  <pre><code>        var dtRow = dtExcel.Columns.Cast&lt;DataColumn&gt;().Where(c =&gt; c.ColumnName != "REQNUM" &amp;&amp; c.ColumnName != "AUTHNUM").ToList();
  
          var checkExcel = dtExcel.Rows.Cast&lt;DataRow&gt;()
              .GroupBy(x =&gt; dtRow.Select(c =&gt; x[c]))
              .Where(g =&gt; g.Count() &gt; 1)
              .Select(gr =&gt; gr);
          //.CopyToDataTable();
  </code></pre>
  
  <p>Thank to Ken for help. This worked great for what I needed. I used the groupby clause so I can combine the duplicate into one row and add the number fields. also group by create a key that I use in an IF statement.</p>
  
  <pre><code>        var dtRow = dtExcel.Columns.Cast&lt;DataColumn&gt;().Where(c =&gt; c.ColumnName != "REQNUM" &amp;&amp; c.ColumnName != "AUTHNUM").ToList();
  
          var excelDup = dtExcel.Rows.Cast&lt;DataRow&gt;()
              .GroupBy(x =&gt; String.Join("", dtRow.Select(c =&gt; x[c])))
              .Select(g =&gt; 
              {
                  var row = g.First();
                  row.SetField("REQNUM", g.Sum(x =&gt; x.Field&lt;double&gt;("REQNUM")));
                  row.SetField("AUTHNUM", g.Sum(x =&gt; x.Field&lt;double&gt;("AUTHNUM")));
                  return row;
              })
              .CopyToDataTable();
  </code></pre>
  
  <p>I also used a where clause to create a variable for datarow compare and no key needed. 
                  //Creates variable with all columns except three. It is used in next query 
                  var dtExcelRow = dtExcel.Columns
                      .Cast().Where(c => c.ColumnName != "TITLE" &amp;&amp; c.ColumnName != "REQSTR" &amp;&amp; c.ColumnName != "AUTHSTR").ToList();
                  var dtListRow = dtList.Columns
                      .Cast().Where(c => c.ColumnName != "TITLE" &amp;&amp; c.ColumnName != "REQSTR" &amp;&amp; c.ColumnName != "AUTHSTR").ToList();</p>
  
  <pre><code>            // Querys create datarow list for compare
              IEnumerable&lt;DataRow&gt; eRow = dtExcel.AsEnumerable()
                  .Where(w =&gt; dtExcelRow.Select(c =&gt; w[c]).Any())
                  .Select(x =&gt; x);
              IEnumerable&lt;DataRow&gt; lRow = dtList.AsEnumerable()
                  .Where(w =&gt; dtListRow.Select(c =&gt; w[c]).Any())
                  .Select(x =&gt; x);
  
              // 1st compare gets list of new records that have changes or are new. 2nd is list of old records being change.
              var newRecords = eRow.AsEnumerable().Except(lRow.AsEnumerable(), DataRowComparer.Default);
              var oldRecords = lRow.AsEnumerable().Except(eRow.AsEnumerable(), DataRowComparer.Default);
  </code></pre>