Editor C# .NET Where and OrWhere clause
Editor C# .NET Where and OrWhere clause
david.j.meyer2@boeing.com
Posts: 54Questions: 15Answers: 0
Link to test case: N/A
Debugger code (debug.datatables.net): N/A
Error messages shown: N/A
Description of problem:
I have an array of strings that contain the ID's of records i need to retrieve from the database. How do I create a where clause, what I am doing is not working. The issue is the ID's are generated dynamically with no limit on the number of ID's being requested.
string[] ids= { "3", "4", "5", "6", "7", "8", "9" };
editor.Where(r =>
{
bool firsttime = true;
foreach (var i in ids)
{
if (firsttime)
{
r.Where("ID", i);
}
else
{
firsttime = false;
r.OrWhere("ID", i);
}
}
});
Any help is greatly appreciated.
Replies
The WHERE CLAUSE we are looking for based on the example above is included below.
WHERE ID=3 OR ID=4 OR ID=5 OR ID=6 OR ID=7 OR ID=8 OR ID=9
As i stated earlier, the number of ID's is variable so the solution needs to be dynamic. Thanks again in advance for any help provided.
Hi David,
Try this:
The reason I think this will make a different is that
Editor.Where()
does not automatically do grouping, butQuery.Where()
does (i.e. that innerq.Where()
is going to be wrapped in parenthesis).Allan
This worked perfectly, thanks Allan. It generates an OR condition where clause dynamically enclosed in parenthesis. Here is an example of a where clause generated using the code you provided. I only had 4 ID's in this testcase.
I am including the generated where clause to help anyone else attempting to programmatically generate a dynamic where clause.
Great to hear that did the job - thanks for posting back!
Allan