Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 251147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:34:13+00:00 2026-05-11T21:34:13+00:00

I am trying to do an autocomplete search using a webservice and Linq To

  • 0

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.

 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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:34:13+00:00Added an answer on May 11, 2026 at 9:34 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using jquery ui 1.8 trying autocomplete Everything works apart from that the ui-menu isn't
I'm trying to autocomplete a textbox. I'm retrieving values from Access Database. Just one
I'm trying to implement autocomplete for a textbox using the following code, but it's
I am trying to do the text box autocomplete by using the code below,
I'm trying to build an autocomplete using jQuery plugin autocomplete from this site .
Am using ui autocomplete from http://jqueryui.com/demos/autocomplete/#remote Using PHP in search.php to return results. Am
Here's a jsFiddle that shows what I'm trying to do: http://jsfiddle.net/P3c7c I'm using the
I'm trying to add the selected value from the autocomplete search box to my
Trying to use autocomplete functionality of YUI, we ran into the fact that it's
I`m trying to add autocomplete to a field that is included in a partial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.