I have following entity model:
public class SortableEntity {
public long Id { get; set; }
public int IntProperty { get; set; }
}
And following failing test case:
[Fact]
public async Task TestExpression()
{
Expression<Func<SortableEntity, long>> expression1 = e => e.Id;
Expression<Func<SortableEntity, int>> expression2 = e => e.IntProperty;
var predicateLambda = Expression.Lambda<Func<SortableEntity, bool>>(
Expression.And(
Expression.Equal(
expression1.Body,
Expression.Constant(123L)
),
Expression.GreaterThan(
expression2.Body,
Expression.Constant(456)
)
),
expression1.Parameters[0] // not sure what should go here
);
await _db.SortableEntities.Where(predicateLambda).ToListAsync();
}
It throws following exception:
System.InvalidOperationException
The LINQ expression 'DbSet<SortableEntity>
.Where(s => s.IsDeleted == False)
.Where(s => s.Id == 123 & e.IntProperty > 456)' could not be translated.
I guess the problem is in s => s.Id == 123 & e.IntProperty > 456
, one expression is using s
variable name and other e
variable name. How I can make it work so both expressions use same variable name?