Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

316
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda