简体   繁体   中英

Dynamic Linq Expression for multi-level query

I have the following Linq expression that I would like to build dynamically.

The problem i'm running into is that I can't build the expression tree to accommodate the complex Select/Any statements. I have read that in order to use methods you literally have to invoke the method as in:

 Invoke("Any")

Expression that I need built dynamically:

Expression<Func<TXLifeRequest, bool>> filter = (txreq) => 
txreq.IsDeleted == false &&
txreq.OLifE.Holding.Select(h => h.Policy)
    .Any(p => p.RequirementInfo
        .Any(r => r.ReqStatus.tc == OLI_LU_REQSTAT.OLI_REQSTAT_OUTSTANDING.tc));

var results = db.GetQueryable(filter)
                .Include(r => r.OLifE.Holding.Select(h => h.Policy)
                   .Select(p => p.RequirementInfo)).ToList();

Here are my model classes:

OLI_LU_REQSTAT

public partial class OLI_LU_REQSTAT : BaseType {

    public string tc { get; set; }

    public string Value { get; set; }
}

TXLifeRequest

public partial class TXLifeRequest : BaseEntity
{
    public virtual OLifE OLifE { get; set; }

    ...
}

OLifE

public partial class OLifE : BaseEntity
{
    public virtual List<Holding> Holding { get; set; }
        ...
}

Holding

public class Holding : BaseEntity
{
    public virtual Policy Policy { get; set; }
    ...
}

Policy

public class Policy : BaseEntity
{
    public virtual List<RequirementInfo> RequirementInfo { get; set; }

    ...
}

RequirementInfo

public partial class RequirementInfo : BaseEntity
{
     public virtual OLI_LU_REQSTAT ReqStatus { get; set; }

    ...
}   

Currently I am running a reflection foreach against GetProperty but have not been able to understand the documentation in order to get 3-4 levels down in the object model:

ParameterExpression parameter = Expression.Parameter(typeof(T), "i");
MemberExpression property = Expression.Property(parameter, propertyName);
ConstantExpression constant = Expression.Constant(val, propertyType);


var condition =
    Expression.Lambda<Func<T, bool>>(
        Expression.Equal(
            property,
            constant
        ),
        parameter
    );

result = AppendExpression(result, condition, result);

Update 1.) Added RequirementInfo. Adding all class properties would not make sense as the required class structure is there.

Quite long. I feel it is too much complex to be "maintainable", and if you need to do some changes, it becomes quite difficult. Even making it "dynamic" so that it can be controlled (enabling or disabling parts) is difficult.

Given

// Enumerable.Any()
static readonly MethodInfo anyTSource = (from x in typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public)
                                         where x.Name == nameof(Enumerable.Any)
                                         let args = x.GetGenericArguments()
                                         where args.Length == 1
                                         let pars = x.GetParameters()
                                         where pars.Length == 2 &&
                                             pars[0].ParameterType == typeof(IEnumerable<>).MakeGenericType(args[0]) &&
                                             pars[1].ParameterType == typeof(Func<,>).MakeGenericType(args[0], typeof(bool))
                                         select x).Single();

// Enumerable.Select()
public static readonly MethodInfo selectTSourceTResult = (from x in typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public)
                                                          where x.Name == nameof(Enumerable.Select)
                                                          let args = x.GetGenericArguments()
                                                          where args.Length == 2
                                                          let pars = x.GetParameters()
                                                          where pars.Length == 2 &&
                                                                    pars[0].ParameterType == typeof(IEnumerable<>).MakeGenericType(args[0]) &&
                                                                    pars[1].ParameterType == typeof(Func<,>).MakeGenericType(args[0], args[1])
                                                          select x).Single();

that are the LINQ Enumerable.Any() and Enumerable.Select()

and given your

// txreq => ((txreq.IsDeleted == False) AndAlso txreq.OLifE.Holding.Select(h => h.Policy).Any(p => p.RequirementInfo.Any(r => (r.ReqStatus.tc == OLI_LU_REQSTAT.OLI_REQSTAT_OUTSTANDING.tc))))
string str1 = filter.ToString();

to make a comparison of your expression with a generated expression

with some shortcuts (instead of txreq.IsDeleted == False I use !rxreq.IsDeleted and instead of including OLI_LU_REQSTAT.OLI_REQSTAT_OUTSTANDING.tc I read its value when the expression tree is built and put it in an Expression.Constant() ,

var par = Expression.Parameter(typeof(TXLifeRequest), "txreq");

// txreq.IsDeleted == false (simplified to !txreq.IsDeleted)
var notIsDeleted = Expression.Not(Expression.Property(par, "IsDeleted"));

// r => 
var par4 = Expression.Parameter(typeof(RequirementInfo), "r");

// OLI_LU_REQSTAT.OLI_REQSTAT_OUTSTANDING.tc
var oli_reqstat_outstanding_tc = Expression.Constant(OLI_LU_REQSTAT.OLI_REQSTAT_OUTSTANDING.tc);

// r.ReqStatus.tc == OLI_LU_REQSTAT.OLI_REQSTAT_OUTSTANDING.tc
Expression<Func<RequirementInfo, bool>> any2Lambda = Expression.Lambda<Func<RequirementInfo, bool>>(Expression.Equal(Expression.Property(Expression.Property(par4, "ReqStatus"), "tc"), oli_reqstat_outstanding_tc), par4);

// To check if it is correct
//any2Lambda.Compile();

// p => 
var par3 = Expression.Parameter(typeof(Policy), "p");

// p.RequirementInfo.Any(...)
Expression<Func<Policy, bool>> any1Lambda = Expression.Lambda<Func<Policy, bool>>(Expression.Call(anyTSource.MakeGenericMethod(typeof(RequirementInfo)), Expression.Property(par3, "RequirementInfo"), any2Lambda), par3);

// To check if it is correct
//any1Lambda.Compile();

// h => 
var par2 = Expression.Parameter(typeof(Holding), "h");

// h.Policy
Expression<Func<Holding, Policy>> selectLambda = Expression.Lambda<Func<Holding, Policy>>(Expression.Property(par2, "Policy"), par2);

// To check if it is correct
//selectLambda.Compile();

//txreq.OLifE.Holding
var holding = Expression.Property(Expression.Property(par, "OLifE"), "Holding");

// txreq.OLifE.Holding.Select(...)
var select = Expression.Call(selectTSourceTResult.MakeGenericMethod(typeof(Holding), typeof(Policy)), holding, selectLambda);
var any1 = Expression.Call(anyTSource.MakeGenericMethod(typeof(Policy)), select, any1Lambda);

var and = Expression.AndAlso(notIsDeleted, any1);

Expression<Func<TXLifeRequest, bool>> lambda = Expression.Lambda<Func<TXLifeRequest, bool>>(and, par);

// To check if it is correct and/or use it
//var compiled = lambda.Compile();

if we try lambda.ToString() we get:

txreq => (Not(txreq.IsDeleted) AndAlso txreq.OLifE.Holding.Select(h => h.Policy).Any(p => p.RequirementInfo.Any(r => (r.ReqStatus.tc == "SOMEVALUE"))))

that is similar enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM