Table of Contents

This page has been moved, the new location is https://korzh.com/easyquery/docs/how-to/add-extra-condition?.

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.

Example:

Korzh.EasyQuery.Db.Table table = model.Tables.FindByName("Customers");
query.ExtraTables.Add(table);
 
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.