Skip to content
Vitaliy Fedorchenko edited this page Mar 2, 2017 · 4 revisions

Query class represents abstract data query to relational data source (in most cases, SQL database). It provides database-independent API for composing dynamic, schema-less queries from C# code.

Main Query elements are:

  • Query.Table (represents target table)
  • Query.Condition (tree structure that represents query conditions)
  • Query.Fields (list of columns to select)
  • Query.Sort (list of order by columns)

Query condition might be a single condition (QConditionNode):

var idCondition = new QConditionNode( new QField("id"), Conditions.Equal, new QConst(1) );
var nameCondition = (QField)"name" == (QConst)"Bob"; // use of operators overloads

or compound condition (and/or):

var andGroup = QGroupNode.And( idCondition, nameCondition);
var orGroup = QGroupNode.Or( idCondition, nameCondition);
andGroup = idCondition & nameCondition; // use of '&' operator overload
orGroup = idCondition | nameCondition; // use of '|' operator overload

Subqueries, table aliases

Query class implements IQueryValue and it can be used as operand in the QConditionNode; in case if sub-query should reference upper-query field table aliases should be used:

var q = new Query("Users.u",
  new QConditionNode( (QField)"u.RoleId", Conditions.In,
    new Query("Roles.r", (QField)"r.Name"==(QConst)"Administrator" ).Select("r.Id")
  )
);

Table alias can be specified explicitly in the following way:

var q = new Query( new QTable("Users", "u") );

Custom DB schema without alias can be specified with the same QTable constructor:

var q = new Query( new QTable("dbo.Users", null) );

Joins

Joins are not available on abstract query level; complex SQL SELECT with JOINs may be represented as dataview (read-only "virtual" table) and NReco.Data library provides special mechanism to define these dataviews on application level (they're processed on .NET side, see "Dataview" for more details).

Clone this wiki locally