I am trying to search a database to see if a string contains elements of a list of search terms.
var searchTerms = new List<string> { "car", "232" };
var result = context.Data.Where(data => data.Name.Contains(searchTerms) ||
data.Code.Contains(searchTerms));
This would work if searchTerms was a string but I've been trying to get it to work with a list of strings.
Essentially I need SQL which would say
SELECT * FROM Data
WHERE Name LIKE '%car%'
OR Name LIKE '%232%'
OR Code LIKE '%car%'
OR Code LIKE '%232%'
linq where list contains any in list seems to be the closes thing I could find to the situation.
Where(data => searchTerms.Contains(data.Name) || searchTerms.Contains(data.Code)
only brings back exact matches to the search terms list.
I have tried searching for multiple keyword search in Entity Framework also and have exhausted that effort. Is there any way to achieve what I am aiming for?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…