1

I have two expression, for example:

public Expression<Func<Entity, bool>> EntityFilterForRead
{
    get
    {
        if (User.Roles.Contains(IdentityRole.ADMIN))
        {
            return _ => true;
        }

        return _ => false;
    }
}
    
public Expression<Func<AnotherEntity, bool>> AnotherEntityFilterForRead
{
    get
    {
        if ( ... )
        {
            return _ => true;
        }

        return _ => false;
    }
}

And we have one more expression where we need these two expressions, because ThirdEntity has Entity and AnotherEntity and we included them.

public Expression<Func<ThirdEntity, bool>> FilterForRead
{
    get
    {
        return //Combine two expressions...
        //ThirdEntity have --> ThirdEntity.Entity and ThirdEntity.AnotherEntity
        //and we should return EntityFilterForRead && AnotherEntityFilterForRead
    }
}

I get this exception:

The LINQ expression ... could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information."

I read the whole page and tried everything, but it is not working. Does anybody have a suggestion?

These solutions are not working for me.

The difference is I have different entities.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Amentos
  • 71
  • 8
  • A thing that doesn't work is `return te => FilterForRead.Compile()` using the `FilterForRead` inside the property itself.. You need to look at the [`Expression.Or(...)`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression.or?view=net-8.0) to combine multiple expressions. – Jeroen van Langen Apr 18 '23 at 13:52
  • Sorry i missed an important thing at last Expression. I modified it. – Amentos Apr 18 '23 at 13:59
  • So i should combinate (bool and expression) or (bool and expression) – Amentos Apr 18 '23 at 14:00

0 Answers0