Home > Software design >  Expression Tree calling an Expression with third parameter
Expression Tree calling an Expression with third parameter

Time:07-15

been dealing with Expression Trees the last couple days and found a problem while trying to call nested Expressions with a second parameter. I'm using the following enter image description here

It's a requisite that myVariable goes into XPTO2. And I would like to do the same with XPTO1 for reusibility since if I send it as a parameter from another Expression (like on the first segment of code) it send a "Parameter count mismatch."

Any guidance on what I'm doing wrong? Pretty new to Expressions so I'm still discovering it. Thanks in advance and let me know if you need any more information.

CodePudding user response:

It is not new library which is doing the same thing. I prefer LINQKit.

So if functionality is similar, choose the following:

public static Expression<Func<TEntity, short, TPropertyResult>> XPTO2()
{
    return (x, myVariable) => x.navigationProperty.MyVariableProperty == null ? null : 
        new TPropertyResult
        {
            // provide fields assignments
        };
}

[ReplaceWithExpression(MethodName = nameof(XPTO2))]
public static TPropertyResult FromEntity(TEntity entity, short myVariable)
{
    return XPTO2().Compile().Invoke(entity, myVariable);
}

And use in the following way:

var x = _myRepository.MyFunc(servicoId)
    .AsExpandable()
    .Select(x => ViewModel1.FromEntity(x, myVariable))
    .ToQueryString();
  • Related