Add columns and conditions by code

Here we suppose that our Query object is referenced as query1 and DataModel object as dataModel1.

 private void AddColumnsConditionsByCode() {
 
  query1.Clear();
 
  //create simple column
  Column col = query1.CreateColumn( "Customers.Country", "Country", SortDirection.Ascending);
  query1.Columns.Add(col);
 
  //create aggregate column
  EntityAttr attr = dataModel1.EntityRoot.FindAttribute(EntityAttrProp.Expression, "Orders.Freight");
  col = new DbColumn("Total sum", SortDirection.None);
  col.Expr = new DbAggrFuncExpr(dataModel1, "SUM", new DbEntityAttrExpr(dataModel1, attr));
  query1.Columns.Add(col);
 
  //create conditions
  //here we create condition object
  SimpleCondition cond = new DbSimpleCondition(dataModel1);
 
  //then we search for an entity attribute which will be used in the left side of condition
  attr = dataModel1.EntityRoot.FindAttribute(EntityAttrProp.Expression, "Orders.OrderDate");
 
  //after that we add found entity attribute as first (left side) expression of our condition
  cond.BaseExpr = new DbEntityAttrExpr(dataModel1, attr);
 
  //here we set an operator used in condition. In our case it will be "is less than" (< symbol in SQL syntax)
  cond.Operator = dataModel1.Operators.FindByID("LessThan");
 
  //finally we set the rigth side expression which is some constant value in our case.
  cond.SetValueExpr(1, new ConstExpr(DataType.Date, "2005-01-01"));
  cond.ReadOnly = true;
 
  //when all parts of our condition are ready - we add it to query
  query1.Root.Conditions.Add(cond);
 
  //here is more simple and quicker way to add a condition (same attribute, operator and value)
  query1.Root.AddSimpleCondition("Orders.OrderDate", "DateBeforePrecise", "2005-01-01");
 
  //here is one more example: how to add a group of conditions
  var predicate = query.AddPredicate(query1.Root, query1.Root.Conditions.Count);
  predicate.AddSimpleCondition("Customers.City", "Equal", "London");
  predicate.AddSimpleCondition("Customer.City", "Equal", "NewYork");

 
  //generate SQL statement
  SqlQueryBuilder builder = new SqlQueryBuilder(query1);
  builder.BuildSQL();
  string sql = builder.Result.SQL;
 
 }