I have this query where I want to return results if the search string provided yields data from the ‘FirstName’ and ‘LastName’ column of my database. They each work individually as I can get results for Firstname = ‘john’ and LastName = ‘doe’. I also want to be able to pass in a search string of ‘John doe’ and get results. How would I implement this using .net/linq
snippet:
var query = _context.Players.Where(p => p.Firstname.Contains(searchString.ToLower().Trim()) || p.Lastname.Contains(searchString.ToLower().Trim()));
3
Answers
use
Split
function like the following:Extracted from the official docs:
Separating the input data is also convenient
Created an extension method class to isolate the functional parts of search algo. This is based on pattern matching algo. You can try this one once.