efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Simple expression causes the warning: The LINQ expression '(Not([p].IsDeleted) And Not([p].IsPending))' could not be translated and will be evaluated locally

Open Villason opened this issue 10 years ago • 1 comments

Hi,

I am writing a aspnet 5 web app.

My problem is that as soon as I include an "and" operand in a filter the expression can no longer be evaluated at the SQL server.

warn: Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory[0]
      The LINQ expression '(Not([p].IsDeleted) And Not([p].IsPending))' could not be translated and will be evaluated locally.
warn: Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory[0]
      The LINQ expression 'Take(21)' could not be translated and will be evaluated locally.

This is my custom code that causes the problem:

            Expression<Func<Post, bool>> filter = p => !p.IsDeleted & !p.IsPending;
            var posts = await _repositories.Post.GetAsync(filter, 21);

If I switch to this no warning is issued:

            Expression<Func<Post, bool>> filter = p => !p.IsDeleted;
            var posts = await _repositories.Post.GetAsync(filter, 21);

GetAsync just ends up calling this code:

        protected async Task<IList<TModel>> GetAsync(
            Expression<Func<TModel, bool>> where,
            Expression<Func<TModel, object>> orderBy,
            bool descending,
            int count,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            IQueryable<TModel> query = DbSet;
            if (where != null)
            {
                query = query.Where(where);
            }
            if (orderBy != null)
            {
                query = descending ? query.OrderByDescending(orderBy) : query.OrderBy(orderBy);
            }
            if (count != int.MaxValue)
            {
                query = query.Take(count);
            }
            return await query.ToListAsync(cancellationToken);
        }

Is this a bug?

Edit: I am using RC1 bits.

Villason avatar May 01 '16 18:05 Villason

Forget it. I was using & instead of &&...

Villason avatar May 01 '16 19:05 Villason