I am trying to do an autocomplete search using a webservice and Linq To Sql to access the database.
Here is my code. This returns results that match any of the search terms, I would like to modify this so each result contains all of the search terms.
I'm aware that SQL full text search is probably the most elegant solution, but I'd like to see if this functionality is possible without modifying the database.
Thanks in advance!
string[] searchTerms = searchString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToArray();
IQueryable<AccountResult> results = db.cdAccounts
.Select(x =>
new AccountResult()
{
idAccount = x.id_Account,
AccountName = x.AccountNme,
AccountNumber = x.AccountNum
}).Distinct().OrderBy(x => x.AccountName);
foreach (string searchTerm in searchTerms)
results = results.Where(x => x.AccountName.Contains(searchTerm) || x.AccountNumber.Contains(searchTerm));
return results.OrderBy(x => x.AccountName).Take(40).ToList();