views:

32

answers:

1

Hi folks
I'm trying to refactor a query that currently uses reflection:

var dbObjects = from d in collection  
    where d.GetType().GetProperty("Id").GetValue(d, null) == id  
    select d;

I would like to use dynamic typing to access the property Id on "d" without knowing what type "d" is at compile time. Something like this:

var dbObjects = from (dynamic)d in collection  
    where d.Id == id  
    select d;

Is this possible? ... and out of interest, is it faster, or does the dynamic runtime use reflection under the hood?

Thanks,

Alan

+2  A: 

Dynamic type uses reflection under the hood so it won't be much faster if any. Because of that I think your Linq-To-Sql expression should work fine. You could check that blog post. It seams that DLR is there only to make your code more readable.

Koynov