You should be able to get all elements that have different values ββusing the linq connection in the element name.
var name = xdoc1.Root.Name.ToString(); var id = (xdoc1.Descendants("Id").FirstOrDefault()).Value; var diff = from o in xdoc1.Root.Elements() join n in xdoc2.Root.Elements() on o.Name equals n.Name where o.Value != n.Value select new HistoryFieldChange() { EntityName = name, FieldName = o.Name.ToString(), KindOfChange = "fieldDataChange", ObjectReference = id, ValueBefore = o.Value, ValueAfter = n.Value, };
One of the advantages of this method is that it is easy to parallelize for multi-core machines, just use PLinq and the AsParallel extension method.
var diff = from o in xdoc1.Root.Elements() join n in xdoc2.Root.Elements().AsParallel() on o.Name equals n.Name where o.Value != n.Value ...
Voila, if the request can be parallelized on your computer, then PLinq will automatically process it. This will speed up work with large documents, but if your documents are small, you can get better acceleration by parallelizing the outer loop that calls GetHistoryFieldChanges , using something like Parallel.For.
Another advantage is that you can simply return IEnumerable from GetHistoryFieldChanges , no need to waste time distributing the list, items will be returned as they are listed, and the Linq query will not be executed until then.
IEnumerable<HistoryFieldChange> GetHistoryFieldChanges(...)
Here are a few examples of iterations of the original, Yannick In-order and My non-parallel Linq implementations. Run on my 2.8ghz laptop with this code .
Elapsed Orig 3262ms All Linq 1761ms In Order Only 2383ms
One interesting thing I noticed ... Run the code in debug mode and then release the mode, it is amazing how much the compiler can optimize a clean version of Linq. I think returning IEnumerable helps a lot to the compiler here.