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. Later I used this code to write my experimental and heavily tuned object-to-object mapper.

Here is the GetMatchingProperties() method that makes the work.

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

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

   
var properties = (from s in
sourceProperties
                     
from t in
targetProperties
                     
where
s.Name == t.Name &&
                            s.PropertyType == t.PropertyType
                     
select
s).ToList();
   
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.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    7 thoughts on “Using LINQ and reflection to find matching properties of objects

    • December 26, 2009 at 6:51 pm
      Permalink

      Cool, but you could also use .Intersects() which is the intended method I think. But yours is properly prettier because the extra “where” syntactic sugar. Best regards, Lasse

    • December 26, 2009 at 10:52 pm
      Permalink

      Thanks for feedback, Lasse! I think this where part is pretty important because this somebody may want to add “smarter stuff” there. :)

    • December 27, 2009 at 4:28 pm
      Permalink

      Did anyone actually try this. I just inserted the VB.NET code into a form and it does not compile.

      Error 1 Operator ‘=’ is not defined for types ‘System.Type’ and ‘System.Type’. C:\Visual Studio Projects\TestLinqObjectCompare\TestLinqObjectCompare\Form1.vb 30 35 TestLinqObjectCompare

    • January 14, 2010 at 10:05 pm
      Permalink

      @Lloyd Sheen: In case of VB.NET, use “Is” instead of “=” on Reference Types.

    • February 19, 2010 at 1:56 pm
      Permalink

      Great idea, but will this work over the long run?

    • August 2, 2011 at 4:44 am
      Permalink

      Wirklich ein wahrer Kommentar. Ich muss echt weblogs.asp.net haufiger lesen

    • Pingback:Performance: Using dynamic code to copy property values of two objects | Gunnar Peipman - Programming Blog

    Leave a Reply

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