Search code examples
c#linqdevexpressiqueryabledynamictype

IQueryable from runtime type with possibility to apply Where and Select


I have to generate IQueryable from runtime type to feed it to a component. This component dynamically loads the data, so IEnumerable is not an option.

Type genericType = typeof(XPQuery<>).MakeGenericType(myRuntimeType);
IQueryable dataSource = (IQueryable)Activator.CreateInstance(genericType, 
                                                    new[] { NewOrmSession() });

This works just fine, but I can not apply nor Where nor Select because it is not using generic.

Any other approach is possible, since I only need to get IQueryable with Where and Select as output from method that accepts runtime type and, in a way, structured where clause.


Solution

  • I have managed with that code that I posted. Just added Dynamic Linq library and got .Where and .Select with string parameter and params object array.
    Then I can do something like:
    dataSource.Select("new( FirstName as Name, LastName , Salary)");
    and also
    dataSource.Where("Salary > @0", 1000)

    Also OrderBy is working as well.
    So it is solved.