How to add an extra condition to your query
It’s a quite common situation when the queries generated by 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 two possible ways of resolving this task:
1. ExtraConditions property of Query class
Section titled “1. 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 additional condition. AddSimpleCondition method will take care of everything.
ASP.NET Core project
Section titled “ASP.NET Core project”Here is an example how to add an additional filter by current user in ASP.NET Core project.
In Program.cs:
app.MapEasyQuery(options => { . . . . . options.AddQueryTunerWithHttpContext((manager, httpContext) => { var userId = httpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? ""; var extra = manager.Query.ExtraConditions; extra.Conditions.Clear(); extra.AddSimpleCondition("Employees.EmployeeID", "Equal", userId); });});AddQueryTunerWithHttpContext appeared in version 7.5.1. A few things to know about it:
- Start with
Conditions.Clear(). The tuner runs every time the query is loaded, and it may run more than once for the same query — for example, when the query comes from the cache (StoreQueryInCache). Clearing the extra conditions first means the condition is added once, not twice. - It adds, it doesn’t replace. You can call it several times, and together with
UseQueryTuner. The tuner set byUseQueryTunerruns first, then the added tuners in the order they were added. - No HTTP context, no query. If there is no current
HttpContext(orIHttpContextAccessoris not registered), the tuner throws an exception instead of running the query without the filter.AddEasyQuery()registers the accessor for you. - Asynchronous export is covered. The export job runs in the background with no HTTP context, so it exports the query exactly as it was tuned for the request that started the export.
- Client-side extra conditions can only narrow the result. If the query sent from the client contains extra conditions of its own, they are added after all query tuners have run, as a separate group joined with AND. They can’t cancel or widen your filter.
- Value lists are not filtered. The tuner changes the query, not the lists of values for lookup attributes (
GetValueList).
ASP.NET MVC (ASP.NET 4.x)
Section titled “ASP.NET MVC (ASP.NET 4.x)”AddQueryTunerWithHttpContext is available only for ASP.NET Core. In an ASP.NET MVC 5 (or Web Forms) project use AddQueryTuner and get the current user from HttpContext.Current:
public class AdvancedSearchController : EasyQueryApiController{ protected override void ConfigureEasyQueryOptions(EasyQueryOptions options) { . . . . . options.AddQueryTuner(manager => { var extra = manager.Query.ExtraConditions; extra.Conditions.Clear(); extra.AddSimpleCondition("Users.ID", "Equal", HttpContext.Current.User.Identity.Name); }); }}HttpContext.Current is available only on the thread that handles the request.
2. ”BuildSQLEx()” method of ”SqlQueryBuilder”
Section titled “2. ”BuildSQLEx()” method of ”SqlQueryBuilder””To insert additional 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 to a 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 additional condition using ExtraTables property of DbQuery class. It’s necessary to ensure that all necessary table joins will be included into result SQL.
Example:
var table = model.Tables.FindByName("Customers");query.ExtraTables.Add(table);
var builder = new SqlQueryBuilder(query);builder.BuildSQLEx("", "Customers.CustomerID = 'ALFKI'");