Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

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.

C#

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;

}


VB.NET
Public Function GetMatchingProperties(ByVal source As Object,_
    ByVal target As Object) As IList(Of PropertyInfo)

    If source Is Nothing Then
        Throw New ArgumentNullException("source")
    End If
    If target Is Nothing Then
        Throw New ArgumentNullException("target")
    End If
    
    Dim sourceType = source.GetType()
    Dim sourceProperties = sourceType.GetProperties()
    Dim targetType = target.GetType()
    Dim targetProperties = targetType.GetProperties()
    
    Dim properties = (From s In sourceProperties _
        From t In targetProperties _
        Where s.Name = t.Name AndAlso s.PropertyType = t.PropertyType _
        Select s).ToList()
    Return properties
End Function

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.

kick it on DotNetKicks.com pimp it Progg it 顶 Shout it
Shout it!
Posted: Dec 26 2009, 01:25 PM by DigiMortal | with 21 comment(s)
Filed under: , ,

Comments

Twitter Trackbacks for Using LINQ and reflection to find matching properties of objects - Gunnar Peipman's ASP.NET blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Using LINQ and reflection to find matching properties of objects - Gunnar Peipman's ASP.NET blog         [asp.net]        on Topsy.com

# December 26, 2009 6:28 AM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# December 26, 2009 6:47 AM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# December 26, 2009 6:54 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# December 26, 2009 7:05 AM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# December 26, 2009 7:11 AM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# December 26, 2009 7:25 AM

uberVU - social comments said:

This post was mentioned on Twitter by gpeipman: New blog post: http://tinyurl.com/yce5bla - Using LINQ and reflection to find matching properties of objects

# December 26, 2009 11:01 AM

Lasse Espeholt said:

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 1:51 PM

DigiMortal said:

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

# December 26, 2009 5:52 PM

Lloyd Sheen said:

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

# December 27, 2009 11:28 AM

Marcel Wijnands said:

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

# January 14, 2010 6:05 PM

Gunnar Peipman's ASP.NET blog said:

Today I gave last performance boost to my property values copying mechanism . I would like to thank my

# February 5, 2010 1:14 PM

Gunnar Peipman's ASP.NET blog said:

Today I gave last performance boost to my property values copying mechanism . I would like to thank my

# February 5, 2010 1:22 PM

Gunnar Peipman's ASP.NET blog said:

I wrote some object to object mapping code and introduced it in some of my previous postings about performance

# February 7, 2010 7:10 AM

rob said:

Great, I read it on: weblogs.asp.net/.../using-linq-and-reflection-to-find-matching-properties-of-objects.aspx

And I have a question: How do I copy "properties" from a DataTable? Since this applies alot to transfering data from the DAL to presentation/business layer.

Thanks,

Rob

# February 8, 2010 4:42 AM

Rulett szisztéma said:

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

# February 19, 2010 8:56 AM

Spielautomaten said:

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

# August 1, 2011 12:44 AM

Genf20 Plus reviews said:

Danke, nun endlich habe ich dies in der Tiefe begriffen ;-)                      

# December 12, 2011 2:40 AM

Genf20 Plus reviews said:

Danke, nun endlich habe ich dies in der Tiefe begriffen ;-)                      

# December 12, 2011 2:43 AM

Performance: Using LCG to copy property values of two objects | Gunnar Peipman - Programming Blog said:

Pingback from  Performance: Using LCG to copy property values of two objects   | Gunnar Peipman -  Programming Blog

# May 23, 2013 3:58 PM

Writing object to object mapper: first implementations | Gunnar Peipman - Programming Blog said:

Pingback from  Writing object to object mapper: first implementations   | Gunnar Peipman -  Programming Blog

# May 23, 2013 4:22 PM