Here is a quick extension method to sort a collection by a given property specified though generics. The extension method then uses a hashset to determine duplicates. This is excellent when paired with unique ids.
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
{
HashSet<TKey> knownKeys = new HashSet<TKey>();
foreach (T element in source)
{
if (knownKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
Databinding is a huge part of any MVC or MVVM pattern. Usually this requires using a string representation of a property name in order to determine how a model is bound to its control. Using strings to control this databinding raises a couple key issues.
1.) Refactoring said property will not automatically update the control, nor will you be informed you’re ‘breaking’ the databind by renaming or removing a property.
2.) Finding usages of said property will also not inform you of the databinding. So if you’re wondering how/if a property is used, you might not find out about it until you break it.
These are headaches I prefer to avoid. It’s extra work to back out of changes, and it is also extra work to find and update these string references. I prefer to work less and accomplish more, which is why I looked into Linq for an answer to these issues.
Linq allows us to get the name of a property through an actual use of the property. So when I use resharper or visual studio to look up references, I can see the databinding.
/// <summary>
/// Determines the member name of a field or property in the form of a string, given a
/// strongly-typed expression.
/// </summary>
/// <typeparam name="T">The type of the class containing the field or property.</typeparam>
/// <param name="obj">The object containing the field or property.</param>
/// <param name="expression">The expression that identifies the field or property.</param>
/// <returns>The member name in the form of a string.</returns>
public static string MemberName<T>(this T obj, Expression<Func<T, object>> expression)
{
MemberExpression memberExpression;
if (expression.Body.NodeType == ExpressionType.Convert)
memberExpression = expression.Body.To<UnaryExpression>().Operand.To<MemberExpression>();
else
memberExpression = expression.Body.To<MemberExpression>();
return memberExpression.Member.Name;
}
Usage:
<%= Html.TextBox(Model.MemberName(x => x.Name)%>
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 