Search linq keyword with

Ok guys, that doesn't make sense ...

I have this method:

// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results = Company.List();
// foreach keyword
for (int i = 0; i < searchTerms.Length; i++)
{
    // results = the existing result set - the items that dont meet the current search term results.
    results = (from comp in results
           where comp.Name.Contains(searchTerms[i]
           select comp).ToList();
}

Now the general idea is that from the list of companies I want everything that contains all the keywords in the search expression provided in my text box on ui.

My problem is that it is “Contains” (highlighted above in **) ... if I say “Company” in the line “Name” and I look for “Co.”, I expect this as a result, because it is the name will be but it is not ...

any ideas?

EDIT:

Ok, I found that the problem was case sensitive, so I reorganized the code:

// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results = Company.List();
// foreach keyword
for (int i = 0; i < searchTerms.Length; i++)
{
    // results = the existing result set - the items that dont meet the current search term results.
    results = (
        from comp in results
        where comp.Name.ToLower().IndexOf(searchTerms[i].ToLower()) > -1
        select comp
        ).ToList();
}

To answer some of your feedback below:

- "Test Company 1", , "test" "company" "1" , , "".

, ???... ?

...

  • 1
  • N... , .
  • , .

, , ... , , , ?

:)

2:

, , ( ) :

// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.ToLower().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results;
// results = the existing result set - the items that dont meet the current search term results.
results = (
    from comp in Company.List()
    where searchTerms.All(s => comp.Name.ToLower().IndexOf(s) > -1)
    select comp
    ).ToList();

:)

+3
3

results . , :

string[] searchTerms = ui_txtSearch.Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

var results = (from comp in Company.List()
              where searchTerms.All(s => comp.Contains(s))
              select comp).ToList();

, .

+3

results, . , Co searchTerms, , , , .

+1

"" , . "". , , , , - :

  • Use a list variable to store the results (for example, "finalResults") and query only the source list.

  • Add to created list: finalResults.AddRange ((linq query) .ToList ());

  • filter the final results using a separate sentence to filter out deceptions.

+1
source

All Articles