Skip to content

Binary AND not supported for bitmasks #191

@Toxantron

Description

@Toxantron

Description

I have an entity with a property of type integer which contains a converted bit-flag enum. I want to query entities by comparing that value to a given bit-mask which is an integer as well.

If I replace the given bit-mask with a static integer like 1, everything works fine. The exception thrown by Entity Framework indicates, that effort wrongly converts the given bit-mask parameter to a Nullable<int>.

Exception

Exception message: The binary operator AND is not defined for "System.Nullable`1[System.Int32]" and "System.Int32"

Stack trace:
at System.Linq.Expressions.Expression.GetUserDefinedBinaryOperatorOrThrow(ExpressionType binaryType, String name, Expression left, Expression right, Boolean liftToNull)
   at System.Linq.Expressions.Expression.And(Expression left, Expression right, MethodInfo method)
   at Effort.Internal.DbCommandTreeTransformation.CanonicalFunctionMapper.<>c.<AddBitwiseMappings>b__4_1(EdmFunction f, Expression[] args)
   at Effort.Internal.DbCommandTreeTransformation.CanonicalFunctionMapper.CreateMethodCall(EdmFunction function, Expression[] arguments)
   at Effort.Internal.DbCommandTreeTransformation.TransformVisitor.Visit(DbComparisonExpression expression)
   at Effort.Internal.DbCommandTreeTransformation.TransformVisitor.Visit(DbAndExpression expression)
   at Effort.Internal.DbCommandTreeTransformation.TransformVisitor.Visit(DbFilterExpression expression)
   at Effort.Internal.DbCommandTreeTransformation.TransformVisitor.Visit(DbProjectExpression expression)
   at Effort.Internal.CommandActions.QueryCommandAction.ExecuteDataReader(ActionContext context)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

Project

public class MyEntity
{
    public int BitValues { get; set; }
}

public void Filter()
{
    DbSet<MyEntity> set = context.MyEntities;

    // Throws exception
    var bitMask = 1;
    var query = (from entity in set
                        where (entity.Bitvalues & bitMask) == bitMask
                        select entity).ToList();

    // Works fine
    query = (from entity in set
                        where (entity.Bitvalues & 1) == 1
                        select entity).ToList();
}

Further technical details

  • EF version: 6.3
  • EF Effort version: 2.2.6
  • Database Provider: NMemory 3.1.0

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions