I'm trying to build a Lambda Expression which is a little complex and I'm having difficulties.
The goal is this expression:
items = items.Where(x => sValues.Any(y => x.Contract_Rates.Select(z => z.CostCenterId).Contains(y)));
However, in RunTime
, I do not know the name of the list that in the example is Contract_Rates
, the issue is that I need to scan the collections of an object, I already have a method that fills the variable Collections
in the code below) and check if this is a collection that implements IHasCostCenter
, if positive, make Lambda Expression
quoted.
What I have so far is:
var sValues = User.CostCenters.Select(x => x.CostCenterId).ToList();
ParameterExpression Parameter = Expression.Parameter(typeof(Contrato), "x");
foreach (var item in Collections.Select(x => x).Where(x => x.GetInterfaces().Any(y => y == typeof(IHasCostCenter))))
{
// Obtenho o nome da lista (i.e. 'Contract_Rates')
var PropertyName = typeof(T).GetProperties().FirstOrDefault(x => x.Name.Contains(item.Name)).Name;
Expression Property = Expression.Property(Parameter, typeof(T).GetProperty(PropertyName));
LambdaExpression Column = Expression.Lambda(Property, Parameter);
Expression Select = Expression.Call(
typeof(Queryable),
"Select",
new Type[] { items.ElementType, Column.Body.Type },
items.Expression,
Column);
Expression Contains = Expression.Call(
typeof(Enumerable),
"Contains",
new[] { sValuesType },
Expression.Constant(sValues, sValues.GetType()),
Select);
MethodCallExpression Where = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { items.ElementType },
items.Expression,
Expression.Lambda(Contains, Parameter));
items = items.Provider.CreateQuery<T>(Where);
But I can not properly develop how I get the SELECT of the expression x.Contract_Rates.Select(y => y.CostCenterId)
and how do I get the ANY of the expression sValues.Any(y => x.Contrato_Rateios.Select(z => z.CentroCustoId).Contains(y))
Any help will be appreciated.