score:1
Accepted answer
I ended up doing using linq to search for the fist term, then if there were multiple terms. I'm open to improvements/optimizations.
//...
if (searchTerms.Count() > 1)
{
List<string> remainingSearchTerms = new List<string>();
for (int x = 1; x < searchTerms.Count(); x++)
remainingSearchTerms.Add(searchTerms[x]);
List<SearchResult> matchingResults = new List<SearchResult>();
foreach (SearchResult result in allResults)
if (MatchSearchTerms(new string[] { result.Name, result.Number, result.id.ToString() }, remainingSearchTerms.ToArray()))
matchingResults.Add(result);
return matchingResults.OrderBy(x => x.Name).Take(MaxResults).ToList();
}
else
return allResults.OrderBy(x => x.Name).Take(MaxResults).ToList();
//...
private bool MatchSearchTerms(string[] searchFields, string[] searchTerms)
{
bool match = true;
foreach (string searchTerm in searchTerms)
{
if (match)
{
bool fieldMatch = false;
foreach (string field in searchFields)
{
if (field.ToLower().Contains(searchTerm.ToLower()))
{
fieldMatch = true; //Only one field needs to match the term
break;
}
}
match = fieldMatch;
}
else
break;
}
return match;
}
score:0
I don't know linq to sql, but it looks like you are reassigning results in your for each loop when you should be appending to results.
Source: stackoverflow.com
Related Articles
- Linq To Sql Search Multiple Columns and Multiple Words
- linq search multiple columns
- Linq to Entites - Multiple columns search
- LINQ search by multiple columns
- Linq wild card search with Sql Functions.Pat Index for multiple columns
- LINQ to Entities search multiple columns, order by total weight of the matches columns
- LINQ search on multiple columns and determine on what column it matched on
- Need to search multiple words using linq
- Linq search multiple columns with multiple word search term
- In Linq for Entity Framework, what is the best practice for returning a resultset by querying multiple columns for a search string?
- C# Linq Group By on multiple columns
- Linq Syntax - Selecting multiple columns
- linq to sql join on multiple columns using lambda
- Distinct on Multiple Columns Entity Framework LINQ
- LINQ - group/sum multiple columns
- Selecting multiple columns with linq query and lambda expression
- how to group by multiple columns using linq
- LINQ to DataSet, distinct by multiple columns
- Dynamic LINQ GroupBy Multiple Columns
- LINQ to SQL - How to efficiently do either an AND or an OR search for multiple criteria
- LINQ to SQL: Left join on multiple columns
- Does this LINQ code perform multiple lookups on the original data?
- LINQ query to match multiple words
- Linq GroupBy on multiple columns with potentials null value
- c # using linq to group by multiple columns in a datatable
- Unwieldy LINQ statement with multiple search criteria
- LINQ COUNT on multiple columns
- How to reuse a linq expression for 'Where' when using multiple source tables
- Linq to SQL select multiple columns
- Group by multiple columns linq count nested row
- LINQ query OR Expression and using one Query Variable
- How to get a count of every single element in an xml file using linq to xml?
- Left join multiple IList<> using Linq
- LINQ select query
- Finding list of unique subsets of a given list of integers
- Linq where keyword vs. Where extension and Expression parameters
- Nhibernate 3 Linq - inner joins
- The fastest way to find dictionary keys that are not exist in another list
- How can you handle an IN sub-query with LINQ to SQL?
- Entity Framework: Why can I not empty a collection in this Many-to-Many relations?
- Pass value from EditorFor to method
- Is It Possible to do this T-SQL Query with LINQ (to Entities)
- CS1061: "IEnumerable<DB-entity-model>" does not contain a definition for 'OrderByDescending()'
- Find value via property name in json.net?
- LINQ get the average of columns and write to csv
- Adding A Custom Property To Entity Framework?
- Entity Framework navigation with shared object table
- Equivalent of outer join with non-equals predicate in Linq
- Searching objects from c# list between date range - Performance Oriented
- EF core 2.1 Upgrade Null reference exception even after checking for not null