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

468
Vistas
Mongodb C# FindAsync. Filter on list inside document using linq

I have a mongodb and I want to filter on a value inside a list in my documents.

My documents looks something like this:

{"_id": "guid" , "mylist": {"stuff": "a", "morestuff": "b"} }

I would like to find a document where "stuff" inside "mylist" is "a" by using linq expressions in FindAsync method.

My best effort so far:

collection.FindAsync(item => item.mylist.Where(item2 => item2.stuff == "a") )

Unfortunatly C# will not accept this statement and i am getting the following errors:

Cannot implicitly convert type "System.Collections.Generic.IEnumerable" to "bool"

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type"

I am relatively new to linq and have mostly been using resharper to do them for me, so I am probably missing something very basic here.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Sorry, i misanderstood your question, you just should replace Where (that gives you collection of items that pass to your expression) with Any that gives true back if there is any item in collection for that expression is true.

Use following query:

collection.FindAsync(Builders<YourClass>.Filter.ElemMatch(
                                     f=>f.mylist, item2=>item2.stuff=="a"))

I think, this one will work too:

collection.FindAsync(x=>x.mylist.Any(b=>b.stuff=="a"))
over 4 years ago · Santiago Trujillo Denunciar
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