4

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

1
  • 2
    How is this linq-to-sql? The database can't call those reflection methods. Commented Jun 24, 2010 at 13:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.