private static void BuildCondition(SqlQuery query, QueryConditionDef condition, DocDef docDef, SqlQueryCondition parentCondition) { if (condition.Condition != ConditionOperation.Include && condition.Condition != ConditionOperation.Exp) { if (condition.SubQueryDef != null) { var subQuery = Build(query.DataContext, condition.SubQueryDef); if (String.IsNullOrEmpty(condition.AttributeName)) { query.AddCondition(condition.Operation, docDef, condition.AttributeId, condition.Condition, subQuery, condition.SubQueryAttribute, parentCondition); } else { query.AddCondition(condition.Operation, docDef, condition.AttributeName, condition.Condition, subQuery, condition.SubQueryAttribute, parentCondition); } } else if (String.IsNullOrEmpty(condition.AttributeName)) { query.AddCondition(condition.Operation, docDef, condition.AttributeId, condition.Condition, condition.Values, parentCondition); } else { query.AddCondition(condition.Operation, docDef, condition.AttributeName, condition.Condition, condition.Values, parentCondition); } } else if (condition.Condition == ConditionOperation.Include && condition.Conditions != null && condition.Conditions.Count > 0) { var attrDef = String.IsNullOrEmpty(condition.AttributeName) ? docDef.Attributes.First(a => a.Id == condition.AttributeId) : docDef.Attributes.First( a => String.Equals(a.Name, condition.AttributeName, StringComparison.OrdinalIgnoreCase)); query.JoinSource(query.FindSourceByDocDef(docDef), attrDef.DocDefType.Id, SqlSourceJoinType.Inner, attrDef.Id); foreach (var child in condition.Conditions) { BuildCondition(query, child, attrDef.DocDefType.Id, parentCondition); } } else if (condition.Condition == ConditionOperation.Exp && condition.Conditions != null) { var exp = query.AddExpCondition(condition.Operation, parentCondition); foreach (var child in condition.Conditions) { BuildCondition(query, child, docDef, exp); } } }