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?
The final example is a demonstration that you capture variables, not values. By the time you execute the query,
ihas the valueterms.Length, hence you get a problem. The smallest change would be to use: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
foreachloop, but now it depends on whether you’re using C# 5 or not. In C# 5, you can just use:But in C# 3 or C# 4, that wouldn’t work, because you’d have one
termvariable across the whole loop, and you have to use this instead: