You can easily find the differences between datasets, dataviews, data tables or any collection using Linq and Enumerables. This is useful if you just want to view the changes that have been made, or if you want to determine if the two datasets match each other, say for testing purposes.
Our first example is a simple list. First convert both lists to enumerables. Then intersect the two.
Enumerable<SomeObject> diff = List<SomeObject>.AsEnumerable().Intersect(List<SomeObject>.AsEnumerable());
Our diff enumerable will contain all the elements that are different. If you already have enumerables, then this is as simple as calling intersect. Any elements in the diff list will be the elements that do not occur in both lists.
Assert.IsTrue(diff.Count() == 0);
If we have a count greater than zero, then our two lists do not contain the same elements. So if our resulting count is zero, our two enumerables are equal.
In order to do the same with datasets, dataviews and data tables we’ll first need to reference the DataTable Extensions methods library. This will open up extension methods to convert data tables to enumerables. Once we get our enumerable, we simply call intersect in the same manner as before.
var resultsDiff = DataSet.Tables[0].AsEnumerable().Intersect(DataSet.Tables[0].AsEnumerable());
Assert.IsTrue(resultsDiff.Count() == 0);
If you’re working with datasets or dataviews, you need to get to the underlying data table in order to use the extension method to convert it into an enumerable.

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 