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 https://github.com/lukemcgregor/LinqExpander
First I was trying to call it as a normal parameter...something like this:
public static Expression<Func<TEntity, TPropertyResult>> XPTO(short myVariable)
and this way I could call my Expression and send my parameter without a problem. But once I call another expression like this inside the first one, it gives an error ("Parameter count mismatch."). So something like this:
ViewModel1
public static Expression<Func<TEntity, TPropertyResult>> XPTO1(short myVariable)
{
return x => x.navigationProperty.MyVariableProperty== null ? null : new TPropertyResult()
(
ViewModel1FieldViewModel2 = x.DataModel2Field1.AsQueryable().Select(RegistryViewModel.XPTO2()).ToList()
)
}
ViewModel2
public static Expression<Func<TEntity, TPropertyResult>> XPTO2(short myVariable)
{
return x => x.navigationProperty.MyVariableProperty== null ||
(
ViewModel2Field1 = (x.someValue == myVariable) ? true : false
)
}
[ReplaceWithExpression(MethodName = nameof(XPTO2))]
public static TPropertyResult FromEntity(TEntity entity, short myVariable)
{
return XPTO2(myVariable).Compile().Invoke(entity);
}
So I decided to give a try to the Func<in T1, in T2, out TResult> signature. So the above code would look like this
ViewModel1
public static Expression<Func<TEntity,short, TPropertyResult>> XPTO1()
{
return (x, myVariable) => x.navigationProperty.MyVariableProperty== null ? null : new TPropertyResult()
(
ViewModel1FieldViewModel2 = x.DataModel2Field1.AsQueryable().Select(RegistryViewModel.XPTO2()).ToList()
)
}
public static TPropertyResult FromEntity(TEntity entity, short myVariable)
{
return XPTO1().Compile().Invoke(entity, myVariable);
}
ViewModel2
public static Expression<Func<TEntity, short, TPropertyResult>> XPTO2()
{
return (x, myVariable) => x.navigationProperty.MyVariableProperty== null ||
(
ViewModel2Field1 = (x.someValue == myVariable) ? true : false
)
}
[ReplaceWithExpression(MethodName = nameof(XPTO2))]
public static TPropertyResult FromEntity(TEntity entity, short myVariable)
{
return XPTO2().Compile().Invoke(entity, myVariable);
}
And I'm trying to call XPTO1 on my Service like this
var x = _myRepository.MyFunc(servicoId).AsExpandable().Select(ViewModel1.XPTO1(), myVariable).ToQueryString();
Which clearly is wrong since VS gives me the following error, but I can't figure out how to call the Expression like this, but I'm not understanding how this should be done.
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.