private static LinqToSqlResult GetDynamicSql <T>(Expression <Func <T, bool> > expression, string headSql) { var tableName = SqlMapperExtensions.GetTableName(typeof(T)); var queryProperties = new List <QueryParameter>(); var body = (BinaryExpression)expression.Body; IDictionary <string, Object> expando = new ExpandoObject(); var builder = new StringBuilder(); // walk the tree and build up a list of query parameter objects // from the left and right branches of the expression tree WalkTree(body, ExpressionType.Default, ref queryProperties); // convert the query parms into a SQL string and dynamic property object builder.Append(headSql + " "); builder.Append(tableName); builder.Append(" WHERE "); for (int i = 0; i < queryProperties.Count(); i++) { QueryParameter item = queryProperties[i]; if (!string.IsNullOrEmpty(item.LinkingOperator) && i > 0) { builder.Append(string.Format("{0} {1} {2} @{1} ", item.LinkingOperator, item.PropertyName, item.QueryOperator)); } else { builder.Append(string.Format("{0} {1} @{0} ", item.PropertyName, item.QueryOperator)); } expando[item.PropertyName] = item.PropertyValue; } return(new LinqToSqlResult(builder.ToString().TrimEnd(), expando)); }
public static string GetTableName <T>(this IDbConnection connection) { var type = typeof(T); return(SqlMapperExtensions.GetTableName(type)); }