[CSHARP-3900] byte[] array access is not supported in LINQ Created: 12/Oct/21  Updated: 27/Oct/23  Resolved: 18/Oct/21

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

Type: Bug Priority: Unknown
Reporter: Boris Dogadov Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The following code:

class A
{
    public byte[] ByteArray {get; set;}
}
collection.AsQueryable().Where(o => o.ByteArray[0] == 1).ToCursor();

Fails with
LINQ2: 

System.InvalidOperationException: 'Convert({document}.ByteArray[0], Int32) is not supported.'

LINQ3:

System.InvalidOperationException: MongoDB.Bson.Serialization.Serializers.ByteArraySerializer must implement IBsonArraySerializer to be used with LINQ.

Other integer types work as expected.



 Comments   
Comment by Robert Stam [ 14/Oct/21 ]

This is currently not supported because a `byte[]` is compactly serialized as a `BsonBinaryData` value instead of as a `BsonArray`.

We could add support to the `ByteArraySerializer` to use `BsonArray` as the serialized representation. Note that this representation would use much more space, because each byte would be serialized as 32-bit integer (because that's the smallest integer type BSON has).

If the `byte[]` were in fact serialized as a `BsonArray` we could support this `byte[]` query in the same way as arrays of other numeric types.

Alternatively, if we could find some MQL operator that would allow us to extract one byte from a `BsonBinaryData` value we could use that to support this query, but I can't find such an MQL operator and I don't think it exists.

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