[CSHARP-2003] $bitsAnySet and other bitwise expressions throw an Exception Created: 15/Jun/17  Updated: 28/Oct/23  Resolved: 04/Jul/22

Status: Closed
Project: C# Driver
Component/s: Linq, LINQ3
Affects Version/s: 2.4.3, 2.4.4
Fix Version/s: 2.17.0

Type: Bug Priority: Major - P3
Reporter: qaqz111 Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win10x64pro
VS2017


Epic Link: CSHARP-3615

 Description   

enum BE
{
    b0 = 1<<0,
    b1 = 1<<1,
    b2 = 1<<2,
    b3 = 1<<3,
    b4 = 1<<4,
}
 
class BBB
{
    public BE be;
}
 
public void foo(IMongoCollection coll)
{
    coll.InsertMany(new []
    {
        new BBB{be=BE.b1},
        new BBB{be=BE.b2},
        new BBB{be=BE.b3},
        new BBB{be=BE.b4},
    });
 
    var mask = BE.b1 | BE.b3;
 
    var qb1 = coll.FindSync(b => (b.be & mask) != 0); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
    var qb2 = coll.FindSync(b => (b.be & mask) == 0); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
    var qb3 = coll.FindSync(b => (b.be & mask) != mask); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
    var qb4 = coll.FindSync(b => (b.be & mask) == mask); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
 
    var q1 = coll.AsQueryable().Where(b => (b.be & mask) != 0).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
    var q2 = coll.AsQueryable().Where(b => (b.be & mask) == 0).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
    var q3 = coll.AsQueryable().Where(b => (b.be & mask) != mask).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
    var q4 = coll.AsQueryable().Where(b => (b.be & mask) == mask).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
}

Looks like the behavior is different from MongoDB C# Driver documents.



 Comments   
Comment by Githook User [ 04/Jul/22 ]

Author:

{'name': 'Robert Stam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-2003: $bitsAnySet etc should work with Enums. (#839)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/57bae35f86a1eade079ca1507786191e97454069

Comment by qaqz111 [ 15/Jun/17 ]

I'm sorry code breaks. But how to edit a posted issue?

    enum BE
    {
        b0 = 1<<0,
        b1 = 1<<1,
        b2 = 1<<2,
        b3 = 1<<3,
        b4 = 1<<4,
    }
    class BBB
    {
        public BE be;
    }
    public void foo(IMongoCollection<BBB> coll)
    {
        var coll = db.GetCollection<BBB>("BBB1");
        coll.InsertMany(new []
        {
            new BBB{be=BE.b0},
            new BBB{be=BE.b1},
            new BBB{be=BE.b2},
            new BBB{be=BE.b3},
            new BBB{be=BE.b4},
        });
 
        var mask = BE.b1 | BE.b3;
 
        var qb1 = coll.FindSync(b => (b.be & mask) != 0); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
        var qb2 = coll.FindSync(b => (b.be & mask) == 0); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
        var qb3 = coll.FindSync(b => (b.be & mask) != mask); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
        var qb4 = coll.FindSync(b => (b.be & mask) == mask); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
 
        var q1 = coll.AsQueryable().Where(b => (b.be & mask) != 0).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
        var q2 = coll.AsQueryable().Where(b => (b.be & mask) == 0).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
        var q3 = coll.AsQueryable().Where(b => (b.be & mask) != mask).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
        var q4 = coll.AsQueryable().Where(b => (b.be & mask) == mask).ToList(); //System.InvalidOperationException: 'Convert(Convert((Convert({document}{be}) & 10))) is not supported.'
    }

Generated at Wed Feb 07 21:41:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.