Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am not sure I understood you correctly but you could approach this with the following logic:</p> <ol> <li>Get the rows that represent the history of an item and order by date descending</li> <li>Get the first row from above as the last change</li> <li>Get the second row from 1. as the previous to last change</li> <li>Compare the data</li> </ol> <p>Here's a potential approach for this (using Linq):</p> <pre><code>var history = db.History.Where(item =&gt; item.ProjId == 1) .OrderByDescending(item =&gt; item.Date); var lastChange = history.First(); var previousChange = history.Skip(1).First(); </code></pre> <p>Now you need to send the above rows to your comparison method. If you want to highlight the changes, you can iterate through properties of the rows and compare values for same properties like this:</p> <pre><code>private IEnumerable&lt;Tuple&lt;string, object, object&gt;&gt; GetChangesBetweenRows(History row1, History row2) { var result = new List&lt;Tuple&lt;string, object, object&gt;&gt;(); var properties = lastChange.GetType().GetProperties(); // both rows are of the same type foreach(var propInfo in properties) { var obj1 = propInfo.GetValue(lastChange, null); var obj2 = propInfo.GetValue(previousChange, null); if(obj1 != obj2) result.Add(Tuple.Create(propInfo.Name, obj1, obj2)); } return result; } </code></pre> <p><strong>EDIT</strong> Given the method above, you can iterate through a collection of history rows and get differences between any of two rows in the collection:</p> <pre><code>static void Main(string[] args) { var history = db.History.Where(item =&gt; item.ProjId == 1) .OrderBy(item =&gt; item.Date) .ToArray(); for(int i=1; i&lt;history.Length; i++) { var diff = GetChangesBetweenRows(history[i-1], history[i]); DisplayDifferences(diff); } } static void DisplayDifferences(IEnumerable&lt;Tuple&lt;string, object, object&gt;&gt; diff) { foreach(var tuple in diff) { Console.WriteLine("Property: {0}. Object1: {1}, Object2: {2}",tuple.Item1, tuple.Item2, tuple.Item3); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload