Using LINQ and reflection to find matching properties of objects

As a side product of some experiments I wrote simple LINQ query that matches properties of two objects and returns these properties as list. The code I wrote is pretty simple and I packed it for you as method.

public IList<PropertyInfo> GetMatchingProperties(object source, object target)
if (source == null
throw new ArgumentNullException("source"
if (target == null
throw new ArgumentNullException("target"

sourceType = source.GetType();
sourceProperties = sourceType.GetProperties();
targetType = target.GetType();
targetProperties = targetType.GetProperties();

var properties = (from s in
from t in
s.Name == t.Name &&
                            s.PropertyType == t.PropertyType
return properties;

The method returns only those properties that match by name and type. If both objects have property called Sum and both of them have Sum in different type (let’s say float and decimal) then this property is not returned by this method. Of course, you can extend this method if you like and you can make it much more clever. Simple implementation given here worked for me very well.

One thought on “Using LINQ and reflection to find matching properties of objects

Leave a Reply

Your email address will not be published. Required fields are marked *