I'm questioning if this if the most efficient solution.
The answer depends on the way in which you are measuring the efficiency.
- In terms of CPU cycles, this is the most efficient way
- In terms of maintenance efforts, methods based on reflection would prove more efficient.
You may want to built a hybrid LINQ/Reflection solution to get acceptable efficiency and keep maintainability in place: use reflection to get all your properties that you need to compare, build a LINQ expression tree that compares them one by one, compile it as a lambda, and use the resultant functor for CPU-efficient comparisons.
Here is a sample implementation of the hybrid approach:
public static Func<T,T,bool> MakeComparator<T>() {
var lhs = Expression.Parameter(typeof (T));
var rhs = Expression.Parameter(typeof (T));
var allPropChecks = typeof(T)
.GetProperties()
.Where(p => p.CanRead && p.GetIndexParameters().Length == 0)
.Select(p => Expression.Equal(Expression.Property(lhs, p), Expression.Property(rhs, p)))
.ToList();
Expression compare;
if (allPropChecks.Count == 0) {
return (a,b) => true; // Objects with no properties are the same
} else {
compare = allPropChecks[0];
compare = allPropChecks
.Skip(1)
.Aggregate(compare, Expression.AndAlso);
}
return (Func<T, T, bool>)Expression.Lambda(compare, new[] { lhs, rhs }).Compile();
}
With this method in hand, you can perform comparisons like this:
class Point3D {
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
}
...
// Construct sample objects
var p1 = new Point3D { X = 1, Y = 2, Z = 3};
var p2 = new Point3D { X = 1, Y = 2, Z = 3 };
var p3 = new Point3D { X = 1, Y = 3, Z = 1 };
// Get a comparator
var cmp = MakeComparator<Point3D>();
// Use the comparator to compare objects to each other
Console.WriteLine(cmp(p1, p2));
Console.WriteLine(cmp(p2, p3));
Here is a demo of this approach on ideone.
Note that this implementation is rather simplistic. It uses ==
for all attributes, rather than going for Equals
where appropriate. You can expand upon it by making line 7 more sophisticated.