Problem with .Where multiples in a Query undergoing Orderby C#

Hey guys,

I’m having a problem where I’m setting up a Queryable and setting some conditions on it (with .Where) and at the end I apply an ordering in a DateTimeOffset field and perform a pagination of 10 in 10 items.

The problem itself is that this guy’s result is “sorted” but it looks like it’s somehow grouped by the Where they were applied.

For example:

A, 1, 2023/08/29 12:00
A, 1, 2023/08/29 12:01
A, 1, 2023/08/29 12:02
A, 1, 2023/08/29 12:03
A, 2, 2023/08/29 12:07
A, 2, 2023/08/29 12:07
A, 2, 2023/08/29 12:08
A, 1, 2023/08/29 12:04
A, 1, 2023/08/29 12:05
A, 2, 2023/08/29 12:10
A, 2, 2023/08/29 12:11
A, 2, 2023/08/29 12:11
A, 1, 2023/08/29 12:06
A, 1, 2023/08/29 12:09

And so it goes through the entire query.

Can you give me a light at the end of the tunnel for me?

Here is the expression extracted via Debug

.Call System.Linq.Queryable.Take(
    .Call System.Linq.Queryable.Skip(
        .Call System.Linq.Queryable.OrderByDescending(
            .Call System.Linq.Queryable.Where(
                .Constant<MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2[Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log,Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log]>(edocs.edocs_padrao_Log.Aggregate([])),
                '(.Lambda #Lambda1<System.Func`2[Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log,System.Boolean]>)),
            '(.Lambda #Lambda2<System.Func`2[Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log,System.DateTimeOffset]>)),
        40),
    10)

.Lambda #Lambda1<System.Func`2[Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log,System.Boolean]>(Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log $log)
{
    .Call System.Linq.Enumerable.Contains(
        .Constant<Senior.SapiensNfe.DataAccess.DaoMongo.Sistema.LogDao+<>c__DisplayClass2_2>(Senior.SapiensNfe.DataAccess.DaoMongo.Sistema.LogDao+<>c__DisplayClass2_2).valores,
        ($log.Mensagem).TipoLog)
}

.Lambda #Lambda2<System.Func`2[Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log,System.DateTimeOffset]>(Senior.SapiensNfe.DataAccess.DadosMongo.Sistema.Log $var1)
{
    ($var1.Mensagem).DataHora
}

PS: I am using Driver version 2.20.0

One new piece of information, I found that the Index in the collection has an impact on the problem, why I don’t know.

I also updated to 2.21, something different happened, if I filter only one field and without any index, it works correctly, but if I change the ordering from desc to asc, it happens again