Consider the following. (which works, but I need Accounts to be passed in as a string. Accounts is list of Account)
repo = repo.Where(x => x.Accounts.Any(p => p.Id == 1));
Here is what I have so far, but I can't seem to wire them together at the end.
var parameterExp = Expression.Parameter(typeof(Car), "x");
Expression propertyExp = Expression.Property(parameterExp, "Accounts");
//Type elementType = propertyExp.Type.GetGenericArguments()[0];
MethodInfo AnyMethod = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static).First(m => m.Name == "Any");
var parameterExp2 = Expression.Parameter(typeof(Car), "p");
var idExpr = Expression.PropertyOrField(parameterExp2, "Id");
MethodInfo method = typeof(long).GetMethod("Equals", new[] { typeof(long) });
var _relatedToId = Expression.Constant(relatedToId, typeof(long));
var equalsMethodExp = Expression.Call(idExpr, method, _relatedToId);
// This is where it breaks. I can't seem to wire it together correctly.
var call = Expression.Call(
AnyMethod,
propertyExp,
equalsMethodExp);
Expression<Func<Car, bool>> predicate = Expression.Lambda<Func<Car, bool>>(call, parameterExp);
// Need to return x => x.Accounts.Any(p => p.Id.Equals(1))
return predicate;
Thanks in advance
x
andp
– Collogue