Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

467
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!