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

124
Views
Field that contains all the strings of an array

How to make the following Entity framework query in MongoDB C# driver?

Get all objects that contains all keywords from the filter - in its name property

var keywords = filter.Split(' ');
_context.Businesses
            .Where(b => keywords.All(s=>b.Name.Contains(s)))
            .ToList();  

I've tried to excute the same query on mongo but I get that the filter is unsupported

_collection.Find<Business>(b => keywords.All(s => b.Name.Contains(s))).ToList();

The exception

An exception of type 'System.ArgumentException' occurred in MongoDB.Driver.dll but was not handled in user code Additional information: Unsupported filter: All(value(System.String[]).Where({document}{Name}.Contains({document}))).

Solution

I've changed the query as M.Wiśnicki suggested

return _collection.AsQueryable<Business>().AsEnumerable().Where(b => keywords.All(keyword => b.Name.Contains(keyword))).ToList();
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Proposed answer will be ok, but this way you get all data to client, the query could be executed directly on MongoDB:

var results  = _collection.Find(Builders<Business>.Filter.Not(
                   Builders<Business>.Filter.ElemMatch(
                            x=>x.keywords,
                            k=>!k.Name.Contains(s))));

It's a little bit tricky, because we filter all values that not (not contain s), but it works. If you don't have a huge amount of data it's ok to query Enumerable as already proposed.

over 4 years ago · Santiago Trujillo Report

0

In MongoDB you need use strongly-typed collection when you call LINQ queries. So need to use AsEnumerable(), this return output as IEnumerable<out T> and you will be able use LINQ. Your query should look like

var results = Businesses.AsEnumerable().Select(x => x.ToLower())
            .Where(x => keywords.All(x.Contains));
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!