Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

313
Visualizações
C# Mongo Driver: How to query list of lists?

I have two objects

public class ParentObject
{
    [BsonId]
    public Guid Id { get; set; }
    public List<ChildObject> ChildObjects{ get; set; }
}

public class ChildObject
{
    public DateTime DateTime { get; set; }
    public string Message{ get; set; }
}

I want to write a query that will find ParentObject in monogo db with the specific Id order all ChildObjects that are inside of the ParentObject based on ChildObject.DateTime and return first 10 ChildObjects. How to write it so it runs on the server-side? The important thing is that I want to avoid downloading all child ChildObjects to the client.

I wrote this query, but according to the documentation it is run on the client-side which I want to avoid

var result = mongoDb.Collection
                .Find(x=> x.Id == request.Id)
                 .Project(x => x.ChildObjects.OrderBy(c => c.DateTime).Take(10))
                 .FirstOrDefault();

I also tried LINQ but it can not be translated due to unsupported expression tree:

            var result = mongoDb.Collection.AsQueryable()
                .Where(x => x.Id== request.Id)
                .Select(x => x.ChildObjects.OrderBy(c => c.DateTime).Take(10))
            .FirstOrDefault();

The Error Message: System.NotSupportedException : The method OrderBy is not supported in the expression tree: {document}{ChildObjects}.OrderBy(c => c.DateTime).

How to write this query? And how to make sure that it is running on the server-side? Is there a possibility to see the translated query?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda