title: Comparing datarows and output difference .net 4
body: <p>I have not used datatables in while.
  Is there a way that given 2 datatables or even 1 compare all the rows with the same customerId and output the fieldname and row value that has changed.</p>
  
  <p>Need to compare client data in various stages and find which values have changed.</p>
  
  <p>Any suggestions?</p>
  
  <p>code sample</p>
  
  <p>UPDATED.
  I have not found a single question on stackoverflow that shows how to get a difference between 2 tables.
  My solution so far but need to find a way to fill the difference object</p>
  
  <pre><code>            //Now use Except operator to find the data in first set and not in second
          var userDataFirstSet = oldDt
              .AsEnumerable()
              .Except(newDt.AsEnumerable(), DataRowComparer.Default);
  
          //Find data in second and not in first
          var userDataSecondSet = newDt
              .AsEnumerable()
              .Except(oldDt.AsEnumerable(),DataRowComparer.Default);
  
  
          List&lt;DataRow&gt; dataRows = userDataFirstSet
              .Union(userDataSecondSet)
              .ToList();
  
          //Now fill the list with all the differences
         List&lt;Difference&gt;diffs=new List&lt;Difference&gt;();
  
  
  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using System.Text;
  
  namespace ConsoleApplication1
  {
      internal class Program
      {
          private static void Main()
          {
              Customer[] oldCustomersSet =
              {
                  new Customer {CustomerId = 1, Name = "Joe", Surname = "Bloggs", City = "London"},
                  new Customer {CustomerId = 2, Name = "Mark", Surname = "Smith", City = "Manchester"},
                  new Customer {CustomerId = 3, Name = "Emily", Surname = "Blunt", City = "Liverpool"},
              };
  
              Customer[] newCustomersSet =
              {
                  new Customer {CustomerId = 1, Name = "Joe", Surname = "Bloggs", City = "London"},
                  new Customer {CustomerId = 2, Name = "Mark", Surname = "Smithyyy", City = "Manchesteraaa"},
                  new Customer {CustomerId = 3, Name = "Emily", Surname = "Blunt", City = "Liverpool"},
              };
  
              DataTable oldDt = GetDataTable(oldCustomersSet);
              DataTable newDt = GetDataTable(newCustomersSet);
  
              //compare the 2 datatables for each customerId and return ColumnName where the value is different.
              Difference diff=CompareRows(oldDt, newDt);
  
              /*
             //wanted result is
             Difference diff=new Difference();
             diff.CustomerId = 2;
             diff.FieldName = "Surname";
             diff.OldDataRowValue = "Smith";
             diff.NewDataRowValue = "Smithyyy"
              */
  
          }
  
          private static Difference CompareRows(DataTable oldDt, DataTable newDt)
          {
              Difference diff=new Difference();
  
              //Ideally a generic way to find row values that are different without hardcode the rowName.
  
  
  
              return diff;
          }
  
          private static DataTable GetDataTable(Customer[] customers)
          {
              DataTable table = new DataTable();
  
              table.Columns.Add("CustomerId", typeof(Int32));
              table.Columns.Add("Name", typeof(string));
              table.Columns.Add("Surname", typeof(string));
              table.Columns.Add("City", typeof(string));
  
              foreach (Customer customer in customers)
              {
                  table.Rows.Add(customer.CustomerId,
                      customer.Name,
                      customer.Surname,
                      customer.City);
              }
  
              return (table);
          }
  
      }
  
      public class Customer
      {
          public int CustomerId { get; set; }
          public string Name { get; set; }
          public string Surname { get; set; }
          public string City { get; set; }
      }
  
      public class Difference
      {
          public int CustomerId { get; set; }
          public string FieldName { get; set; }
          public string OldDataRowValue { get; set; }
          public string NewDataRowValue { get; set; }
      }
  }
  </code></pre>