I am using ASP.net Core 3.0
with Entity Framework Core 3.0
and Pomelo.EntityFrameworkCore
provider for MySQL
, I need to query all the users that are from specific Towns.
Lets say for example I have a list of strings called targettedTowns
in which I have the following towns
var targettedTowns = new List<string>() {"korangi","landhi","zia colony","shah faisal","quaidabad"};
Now I want to find all the users that are located in the targettedTowns
list using Linq Lambda syntax.
Users in my database have their towns saved like
What I am trying currently is
var users = context.Users.Where(x => x.Town.ToLower().Contains(targettedList)).ToList();
but as String.Contains does not take a list in argument so I cant use this.
You can try using Any
method, I'm not sure whether it's supported but it's worth trying:
var result = context.Data.Where(data => searchTerms.Any(x => data.Name.Contains(x)) ||
searchTerms.Any(x => data.Code.Contains(x));
If this gives you NotSupportedException
you can add AsEnumerable
before Where
to fetch all records and execute the query in memory rather than DB.
I'm late to the party but using the SearchExtensions nuget package you could do something like the following
var result = context.Data.Search(x => x.Name, x => x.Code).Containing(searchTerms);
This builds an expression tree so will still perform the query on the server (and not in memory) and will essentially run the SQL you desire above