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 8965727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:52:21+00:00 2026-06-15T16:52:21+00:00

I’m trying to build a generic query builder for accepting user-entered search terms. To

  • 0

I’m trying to build a generic query builder for accepting user-entered search terms. To get the where clauses I need, I must parse terms and independently determine which field needs to be included in the clause.

Note that this example is greatly simplified to illustrate the point. I know that in this particular case the entire result could be expressed as a single Where() statement. This code will work in my problem space (where a single Where() statement would not), so the answer has to address what’s actually happening under the hood here, not how to make it simpler.

I start with a list of terms (here represented as a string[], but will ultimately be an IList of a more complex type that helps guide query builder):

    string[] terms = new string[] {
        "hope",
        "bob",
    };

This first example (below) gives the right result set (any record that has an employee with ‘bob’ in either of the three searched fields and also has ‘hope’ in either of the three fields matches). This does demonstrate that the proper query is built from the code when chaining Where() clauses:

    var query0 = Sites.Where(s => s.SiteId < 200);
    query0 = query0.Where(s =>
        s.Employee.FirstName.Contains(terms[0]) ||
        s.Employee.LastName.Contains(terms[0]) ||
        s.Employee.Username.Contains(terms[0]));
    query0 = query0.Where(s =>
        s.Employee.FirstName.Contains(terms[1]) ||
        s.Employee.LastName.Contains(terms[1]) ||
        s.Employee.Username.Contains(terms[1]));
    query0.Dump();

(Note that I can’t use this direct approach because I don’t know how many terms there will be, and some of them will be on Employee while others will be on other fields of ‘Sites’, so at compile time I must be able to iterate and treat each term uniquely.)

This next example (below) is what I want to do, but it doesn’t honor the first term, and matches only on the last; any record that has ‘bob’ in any of the fields is included regardless of whether or not ‘hope’ appears in any field:

    var query1 = Sites.Where(s => s.SiteId < 200);
    foreach (string term in terms)
    {
        query1 = query1.Where(s =>
            s.Employee.FirstName.Contains(term) ||
            s.Employee.LastName.Contains(term) ||
            s.Employee.Username.Contains(term));
    }
    query1.Dump();

This final example (below) gives an ‘index out of bounds’ error when it reaches the Dump() instruction:

    var query2 = Sites.Where(s => s.SiteId < 200);
    for (int i = 0; i < terms.Length; ++i)
    {
        query2 = query2.Where(s =>
            s.Employee.FirstName.Contains(terms[i]) ||
            s.Employee.LastName.Contains(terms[i]) ||
            s.Employee.Username.Contains(terms[i]));
    }
    query2.Dump();

I think query2 is most telling example. It’s almost as if LINQ is trying to bind variables to SQL parameters after the entire query has been built, and it’s trying to use i==2 (which would be the value of i at the instant the loop exits) for all bindings. This would also be consistent with the result I am seeing with query1.

Does anyone know how binding works and how I can construct my query?

  • 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-06-15T16:52:22+00:00Added an answer on June 15, 2026 at 4:52 pm

    The final example is a demonstration that you capture variables, not values. By the time you execute the query, i has the value terms.Length, hence you get a problem. The smallest change would be to use:

    for (int i = 0; i < terms.Length; ++i)
    {
        int copy = i;
        query2 = query2.Where(s =>
            s.Employee.FirstName.Contains(terms[copy]) ||
            s.Employee.LastName.Contains(terms[copy]) ||
            s.Employee.Username.Contains(terms[copy]));
    }
    

    Now each iteration of the loop has a separate variable called copy, which isn’t changed – so you’re good.

    Now, it would be cleaner to use a foreach loop, but now it depends on whether you’re using C# 5 or not. In C# 5, you can just use:

    // Only works in C# 5
    foreach (string term in terms)
    {
        query2 = query2.Where(s =>
            s.Employee.FirstName.Contains(term) ||
            s.Employee.LastName.Contains(term) ||
            s.Employee.Username.Contains(term));
    }
    

    But in C# 3 or C# 4, that wouldn’t work, because you’d have one term variable across the whole loop, and you have to use this instead:

    // Works in C# 3+
    foreach (string term in terms)
    {
        string copy = term;
        query2 = query2.Where(s =>
            s.Employee.FirstName.Contains(copy) ||
            s.Employee.LastName.Contains(copy) ||
            s.Employee.Username.Contains(copy));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
This could be a duplicate question, but I have no idea what search terms
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.