I have the following Expression tree which gives me an error when I compile the expression tree.
string tenantId = "tst user";
Expression<Func<MongoIdentityUser, bool>> filterToUse = t => t.IsActive == true && t.IsApproved == true;
var expConst = Expression.Constant(tenantId);
var paramExp = Expression.Parameter(typeof(MongoIdentityUser), "t");
var callExp = Expression.PropertyOrField(paramExp, "TenantId");
var equalExp = Expression.Equal(callExp,Expression.Constant(null));
var equalExp2 = Expression.Equal(callExp, expConst);
var conditionExp = Expression.Condition(equalExp, Expression.Constant(true), equalExp2);
var AndExp = Expression.AndAlso(filterToUse.Body, conditionExp);
var lambdaExp1 = Expression.Lambda<Func<MongoIdentityUser, bool>>(AndExp, paramExp);
Console.WriteLine(lambdaExp1.Compile());
The generated expression is as follows
t => (((t.IsActive == True) AndAlso (t.IsApproved == True)) AndAlso IIF((t.TenantId == null), True, (t.TenantId == "tst user")))
But when I called lambdaExp1.Compile() it gives me the following error