private static void GetCondition <T>(string linkOperator, string[] arrUnary, ref string condition, ref SQlQuery query) { switch (arrUnary.FirstOrDefault()) { case "Not": condition += " NOT {0}"; break; case "IsNullOrEmpty": string value = arrUnary[1].Replace(")", string.Empty); List <string> joinOperator = new List <string>(); string whereCondition = string.Empty; string[] arrValue = value.Split('.'); if (arrValue.Count() > 1) { var replacedTable = value[0]; string newProperties = string.Empty; for (int i = 0; i < arrValue.Count(); i++) { if (i == 0) { continue; } else if (i == arrValue.Count() - 1) { newProperties += arrValue[i]; } else { newProperties += arrValue[i]; newProperties += '.'; } } //string tableName = QueryHelper.GetTableName<T>(); GetJoinOperation(typeof(T), newProperties, ref joinOperator); GetWhereOperation(linkOperator, newProperties, ref whereCondition); } if (!string.IsNullOrEmpty(condition)) { whereCondition = string.Format(condition, whereCondition); } query.AddJoinOperator(joinOperator); query.AddWhereCondition(whereCondition); condition = string.Empty; break; } arrUnary = arrUnary.Where((value, index) => index != 0).ToArray(); if (arrUnary != null && arrUnary.Count() > 1) { GetCondition <T>(linkOperator, arrUnary, ref condition, ref query); } }
private static void WalkTree <T>(BinaryExpression body, ExpressionType linkingType, ref SQlQuery query) { List <QueryParameter> queryProperties = new List <QueryParameter>(); if (body.NodeType != ExpressionType.AndAlso && body.NodeType != ExpressionType.OrElse) { var slides = body.Left.ToString().Split(new char[] { '.' }); string[] conditions = new string[slides.Count()]; GetExactlyNameParam(typeof(T), slides, ref conditions); string propertyName = string.Join(".", conditions.Where(c => !string.IsNullOrEmpty(c))); if (body.Left.NodeType == ExpressionType.Convert) { //Remove the trailing ) when convering. propertyName = propertyName.Replace(")", string.Empty); } List <string> joinOperator = new List <string>(); if (propertyName.Contains('.')) //apply join { GetJoinOperation(typeof(T), propertyName, ref joinOperator); } else { propertyName = query.RootTable + '.' + propertyName; } dynamic propertyValue = body.Right; string opr = GetOperator(body.NodeType); string link = GetOperator(linkingType); string[] elements = propertyName.Split(new char[] { '.' }); if (elements.Count() > 2) { string removed = elements[0]; propertyName = propertyName.Replace(removed + '.', string.Empty); } query.AddQueryParameter(new QueryParameter(link, propertyName, propertyValue.Value, opr)); if (joinOperator != null && joinOperator.Any()) { query.AddJoinOperator(joinOperator); } } else { query = BuildCondition <T>(body.Left, body, query); query = BuildCondition <T>(body.Right, body, query); } }