Dynamic type inside Linq-to-sql query

I am trying to reorganize 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 identifier on "d", not knowing what type of "d" is at compile time. Something like that:

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

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

Thanks,

Alan

+4
source share
1 answer

The dynamic type uses reflection under the hood, so it won’t be much faster if there is one. Because of this, I think your Linq-To-Sql expression should work fine. You can check that blog post . It is a stitch that DLR exists only to make your code more readable.

+2
source

Source: https://habr.com/ru/post/1313746/


All Articles