How to use linq extensions with Func parameters in Dynamic Linq expressions?

dynamic-linq extension-methods linq

Question

While somewhat powerful, the System.Linq.Dynamic library has a surprising lack of documentation, especially in regards to what conventions must be followed for more complex queries.

In a query I'm working on, it contains a FirstOrDefault call, but I can't seem to get it to work.

Here's the whole (unworking) expression:

"Locations.FirstOrDefault(x => x.IsPrimaryLocation).Address1 as Address"

Can I write this FirstOrDefault expression to work with Dynamic linq?

What is the correct way to write this expression?

1
0
10/6/2014 2:17:29 PM

Accepted Answer

Extending the dynamic library is certainly an option as already suggested. an alternative given the Where in Dynamic Linq returns an Iqueryable

 public static class DynamicQueryable {

    public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values) { return (IQueryable<T>) Where((IQueryable) source, predicate, values); }

    public static IQueryable Where(this IQueryable source, string predicate, params object[] values)       {

using a DYnamic Object for the context or repository "locations".
Then use a where which could contain dynamic string predicate and follow with firstOrDefault.
(catch or test for null not considered)

DynamicLocations.Where(x => x.IsPrimaryLocation).FirstOrDefault( ).Address1 as Address;

or dynamic where if needed

DynamicLocations.Where("IsPrimaryLocation",new string[]).FirstOrDefault( ).Address1 as Address;

Details: You can expose on a method on a generic repository Class which you instantiate as a dynamic

     public virtual IQueryable<TPoco> DynamicWhere(string predicate, params object[] values) {
        return AllQ().Where(predicate, values);
    }

Dynamic Generic Repository instantiation Sample

  public class RepositoryFactory<TPoco>  where TPoco : BaseObject,new() {

    public IRepositoryBase<TPoco> GetRepository(DbContext context) {

       // get the Pocotype for generic repository instantiation
        var pocoTypes = new[] {typeof (TPoco)};  // but supports <T,U>
        Type repBaseType = typeof (RepositoryBase<>);

        IRepositoryBase<TPoco> repository = InstantiateRepository(context, repBaseType, pocoTypes);

        return repository;
    }


    private IRepositoryBase<TPoco> InstantiateRepository(DbContext context, Type repType, params Type[] args) {

        Type repGenericType = repType.MakeGenericType(args);
        object repInstance = Activator.CreateInstance(repGenericType, context);
        return (IRepositoryBase<TPoco>)repInstance;
    }

}
1
10/6/2014 8:05:59 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow