Upgrading from 2.17 to 2.19 causes LINQ based queries to fail with not supported expression

Hi, I upgraded from 2.17 to 2.19 the C# driver and I have a query like:

var data = collection
    .AsQueryable()
    .Select(x => new
    {
        A1 = (string)x.Data["a1"],
        B2 = (string)x.Data["b2"],
        C3 = (string)x.Data["c3"],
        D4 = (long?)x.Data["d4"]["d5"],
        E5 = (int?)x.Data["e5"]["e6"],
        F6 = x.Data["f6"]["7"] == null
            ? null
            : ((IEnumerable<BsonValue>)x.Data["f6"]["f7"]).Select(_ => (string)_["f8"])
    })
    .OrderByDescending(x => x.E5)
    .FirstOrDefault();

This worked fine with 2.17, after upgrading (I found out you folks switched to LINQv3 provider which might be root cause) it fails with expression not supported exceptions for Nullable (long? and int?) casts.

Any ideas how to fix it and keep new LINQv3 provider?

I looked at related:

Issue with 2.18 to 2.19 NuGet Upgrade of MongoDB C# Driver

Thank you,
V.

Switching to LINQv2 provider resolves the issue, but I still think this should work with v3.

Hi, @Vedran_Mandic,

Thank you for reaching out to us about this issue. In LINQ2, we discarded all casts in the LINQ AST, which is not always the right thing to do. In LINQ3 we try to be more purposeful when removing casts - only removing them when we know the removal is safe and correct.

Please file a CSHARP bug in our issue tracker with a repro so that we can investigate further. While quickly trying to repro the issue with the code above, it wasn’t clear to me the exact type of the Data property. Providing a self-contained repro with all the necessary data models will assist us greatly in quickly reproducing and investigating this issue.

Sincerely,
James