Search code examples
c#linqdynamiccustom-linq-providers

C# Linq over dynamic table


i would like to make a linq to sql provider that allow to me to query onto a table that is nor mapped in the datamodel nor known.

I only know a table's alias that i use to query another known table for translation (from the alias to the real table name), after that i will using standard linq to query the real table, read data, and put each results into a dynamic's object.

To achieve that i suppose i need to define a custom linq provider that will manipulate the expression tree, and then call standard linq to sql; but at moment i don't know how do it.

So my aim is that i would write code like this :

 List<dynamic> rows = form book in context.Book
                      where book.Author = "Author"
                      select book;

Thank in advance for any suggestion.


Solution

  • You can use Reflection:

    PropertyInfo table = typeof(ContextType).GetProperty(TableName);
    
    from book in table.GetValue(Context)
    ...