Here we suppose that our Query
object is referenced as query1 and DataModel
object as dataModel1.
private void AddColumnsConditionsByCode() { query1.Clear(); EntityAttr attr; attr = dataModel1.EntityRoot.FindAttribute(EntityAttrProp.Expression , "Customers.Country"); //create columns Column col; col = new DbColumn("Country", SortDirection.Ascending); col.Expr = new DbEntityAttrExpr(dataModel1, attr); query1.Columns.Add(col); 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); //generate SQL statement SqlQueryBuilder builder = new SqlQueryBuilder(query1); builder.BuildSQL(); string sql = builder.Result.SQL; }
Discussion