Back to
Latest changes:

This is an old revision of the document!

How to add an extra condition to your query

It's quite common situation when the queries generated with EasyQuery components must have an additional condition(s) not visible to end-users. For example you may need to limit the result set by user ID, department ID or some time frames. EasyQuery provides you with 2 possible ways of resolving this task:

1. ''BuildSQLEx()'' method of ''SqlQueryBuilder''

To insert extra conditions into generated SQL statements you can use BuildSQLEx() function of SqlQueryBuilder class (instead of BuildSQL you are using by default) and pass necessary condition(s) in its second parameter. The value of that parameter will be added into result SQL statement at the end of WHERE clause with AND conjunction to conditions, defined by end-users through visual controls.

The only trick here - you may also need to list all tables which take part in that extra condition using ExtraTables property of DbQuery class. It's necessary to ensure that all necessary table joins will be included into result SQL.


Korzh.EasyQuery.Db.Table table = model.Tables.FindByName("Customers");
SqlQueryBuilder builder = new SqlQueryBuilder(query);
builder.BuildSQLEx("", "Customers.CustomerID = 'ALFKI'");

2. ExtraConditions property of Query class

Since version 3.5.0 of EasyQuery ASP.NET you can use more simple and convenient way of inserting extra conditions:

    query.ExtraConditions.AddSimpleCondition("Customers.CustomerID", "Equal", "ALFKI");

Now there is no need to deal with ExtraTables or use the exact SQL expression for your extra condition. AddSimpleCondition method will take care about everything.


, 2014/02/23 15:28
What will happen if I add a table that has already been included in the query by the user?
, 2014/02/23 21:47
Nothing special. It will be added only once.
Actually, adding table into ExtraTables list is just a guarantee that the table will be included into generated SQL.
, 2014/02/25 11:49
How would I edit the following code to add an extra condition?

protected override string GenerateQueryStatement(Query query) {

SqlQueryBuilder builder = new SqlQueryBuilder((DbQuery)query);
if (builder.CanBuild) {
return builder.Result.SQL;
return string.Empty;
, 2014/02/25 12:21
Hm. It seems the article describes what to do quite clearly.
Just change one call
to something similar to the code listed in this article.
, 2014/02/25 13:47
When I change my code to:

protected override string GenerateQueryStatement(Query query) {

SqlQueryBuilder builder = new SqlQueryBuilder((DbQuery)query);
if (builder.CanBuild) {

Korzh.EasyQuery.Db.Table table = Model.Tables.FindByName("policies");

builder.BuildSQLEx("", "policies.PolicyID = 5");

return builder.Result.SQL;
return string.Empty;

I get the following error:

'Korzh.EasyQuery.Query' does not contain a definition for 'ExtraTables' and no extension method 'ExtraTables' accepting a first argument of type 'Korzh.EasyQuery.Query' could be found (are you missing a using directive or an assembly reference?)
, 2014/02/25 15:20
Just cast query object to DbQuery class:

, 2014/02/25 18:20
That worked. Thanks.
, 2014/05/06 22:57
I have table1 with a path to table2 and the extra condition is on table2, I included table2 on the extratable.
When I don't include any field of table2 on the query it will rise the error "Cannot find a path between tables", if I include in the query a field of table2 the error won't be rise.
What is missing?
, 2014/05/07 00:15
I found the error,
I execute builder.BuildSQLEx("", "Customers.CustomerID = 'ALFKI'");
then I execute builder.BuildSQL();
and there es where the error rise.

Just by executing builder.BuildSQLEx and erasing builder.BuildSQL() does the work.
, 2014/05/07 01:49
Sorry guys, the error is still happening, I didn't find a solution, the problem still persist.
, 2014/05/09 23:02
To find the reason of the problem we need your model and query files (XML).
Send them both to techsupport{at}
, 2014/08/11 13:01
*query.ExtraConditions.AddSimpleCondition("Customers.CustomerID", "Equal", "ALFKI");*

doesnt work well.
if i use it to add a condition it adds an extra "AND" after condition due to which query execution fails.
, 2014/08/12 09:37
Do you mean that you add only this condition (without any others)? If yes - it's not quite correct.
This function is used to add "extra" conditions which are not visible to users but it your query must have at least one "normal" condition to use this method.
, 2014/08/17 10:43
How to Get Model to return the reference of Korzh.EasyQuery.Db.Table.

such as Model Specified in Korzh.EasyQuery.Db.Table table = Model.Tables.FindByName("policies")

Can you please paste the code to get Model, Whenloading entities/Data Model from xml file
, 2014/08/19 16:33
You can get it from Model property of Query object.

DbModel model = (DbModel)query.Model;
, 2014/08/19 20:55
Hi, Thanks for the reply.
Also I have one more query as, How to add extra condition in "ExecuteQuery(string queryJson, string optionsJson)" method of EQMvcDemoEF.NET45 Demo MVC project, Which works on sending json format query to the ExceuteQuery action method. Also can please provide us any idea regrading adding custom paging in the result grid of the tool.
, 2014/09/10 10:02
Utlam, get the latest version of EasyQuery. If you look into ExecuteQuery implementation in EqMvcDemoDB sample project you will see now that the first step there is getting a DbQuery object.
After that you can use this object to add extra conditions.
Enter your comment: