I'm trying to create a DbSet LINQ filter. I'm using expressions so that I can reduce the number of lines of code caused by unique combinations of filter options and filter properties.
I was able to apply this easily for properties of the object in the DbSest (Working Code pasted bellow). But I hit a road block when I was trying to express. dbSet.Where(x => x.listProp.Any(y => y.Name == "val"));
I don't know how to express .Any(...)
while building an expression tree.
Currently I'm trying to follow the solution in the post but I'm getting the following issue when I get to var anyCall = ...
: System.InvalidOperationException: 'No generic method 'Any' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. '
This is what I currently have.
private IQueryable<dbo.Transaction> _FilterTransactionPartyType(ParameterExpression param, IQueryable<Transaction> query, string transactionPartyType)
{
MemberExpression expM2M = Expression.PropertyOrField(param, "Parties");
Type tM2M = typeof(M2M_Transactions_Party);
ParameterExpression paramM2m = Expression.Parameter(tM2M, "m2m");
//Any(m2m => m2m.Party.Name == "dd" && m2m.PartyType.Name = "dd")
System.Reflection.PropertyInfo piParty = tM2M.GetProperty("Party");
System.Reflection.PropertyInfo piPartyName = piParty.PropertyType.GetProperty("Name");
System.Reflection.PropertyInfo piPartyType = tM2M.GetProperty("TransactionPartyType");
System.Reflection.PropertyInfo piPartyTypeName = piPartyType.PropertyType.GetProperty("Name");
MemberExpression expPartyType = Expression.Property(paramM2m, piPartyType);
MemberExpression expPartyTypeName = Expression.Property(expPartyType, piPartyTypeName);
ConstantExpression expPartyTypeValue = Expression.Constant(transactionPartyType, typeof(string));
BinaryExpression isPartyTransactionType = Expression.Equal(expPartyTypeName, expPartyTypeValue);
MemberExpression expParties = Expression.Property(paramM2m, piParty);
MemberExpression expParty = Expression.Property(expParties, piPartyName);
BinaryExpression isParty = _Filter(expParty);
BinaryExpression isPartyAndType = Expression.AndAlso(isParty, isPartyTransactionType);
Expression<Func<M2M_Transactions_Party, bool>> internalLambda = Expression.Lambda<Func<M2M_Transactions_Party, bool>>(isPartyAndType, paramM2m);
// book.properties.Any(bookProperty => bookProperty.type.key == "lingerie" && bookProperty.value == "1")
var anyCall = Expression.Call(
typeof(Queryable), "Any", new[] { tM2M },
expM2M, internalLambda
);
// book => book.properties.Any(...)
var lambda = Expression.Lambda<Func<Transaction, bool>>(anyCall, param);
return query.Where(lambda);
}