/// <summary> /// 构造查询条件 /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> /// <param name="op"></param> /// <param name="paramName"></param> /// <returns></returns> private static WhereClip BuildWhereChip(string propertyName, object value, QueryOper op, string paramName = null) { if (op != QueryOper.IsNull && op != QueryOper.IsNotNull && (value == null || value == DBNull.Value)) { return(null); } WhereClip where = new WhereClip(); StringBuilder sbSql = new StringBuilder($"{propertyName}{op.ToDescription()}"); if (value != null && value != DBNull.Value) { if (paramName == null) { paramName = SqlQueryUtils.GetParmName(propertyName); } if (paramName.Length > 0) { sbSql.Append($"@{paramName} "); where.Parameters.Add(new DataParameter(paramName, value)); } else { sbSql.Append($"{value} "); } } where.WhereSql = sbSql.ToString(); return(where); }
/// <summary> /// Not In (Dapper.Net) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="propertyName"></param> /// <param name="values"></param> /// <param name="paramName"></param> /// <returns></returns> public static WhereClip DapperNotIn <T>(string propertyName, IEnumerable <T> values, string paramName = null) { if (values == null || !values.Any()) { return(null); } WhereClip where = new WhereClip(); if (paramName == null) { paramName = SqlQueryUtils.GetParmName(propertyName); } if (paramName.Length > 0) { where.Parameters.Add(new DataParameter(paramName, values)); where.WhereSql = $"{propertyName} not in @{paramName} "; } else { if (typeof(T).FullName == typeof(string).FullName) { string strIn = string.Join <T>("','", values); where.WhereSql = $"{propertyName} not in ('{strIn}') "; } else { string strIn = string.Join <T>(",", values); where.WhereSql = $"{propertyName} not in ({strIn}) "; } } return(where); }
/// <summary> /// Apply a "between" constraint to the named property /// </summary> /// <param name="propertyName">The name of the Property in the class.</param> /// <param name="lo">The low value for the Property.</param> /// <param name="hi">The high value for the Property.</param> /// <returns>A <see cref="WhereClip" />.</returns> public static WhereClip Between(string propertyName, object lo, object hi, string paramName = null) { WhereClip where = new WhereClip(); StringBuilder sbSql = new StringBuilder($"{propertyName} between "); if (paramName == null) { paramName = SqlQueryUtils.GetParmName(propertyName); } if (paramName.Length > 0) { string strParamName1 = paramName + "_pmin"; string strParamName2 = paramName + "_pmax"; sbSql.Append($"@{strParamName1} and @{strParamName2} "); where.Parameters.Add(new DataParameter(strParamName1, lo)); where.Parameters.Add(new DataParameter(strParamName2, hi)); } else { sbSql.Append($"{lo} and {hi} "); } where.WhereSql = sbSql.ToString(); return(where); }
/// <summary> /// Apply an "in" constraint to the named property (Dapper.Net) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="expression"></param> /// <param name="values"></param> /// <param name="paramName"></param> /// <returns></returns> public static WhereClip DapperIn <TEntity, T>(Expression <Func <TEntity, object> > expression, IEnumerable <T> values, string paramName = null) where TEntity : class { string propertyName = SqlQueryUtils.GetPropertyName(expression); return(DapperIn(propertyName, values, paramName)); }
/// <summary> /// Apply a "between" constraint to the named property /// </summary> /// <param name="expression">The name of the Property in the class.</param> /// <param name="lo">The low value for the Property.</param> /// <param name="hi">The high value for the Property.</param> /// <returns>A <see cref="WhereClip" />.</returns> public static WhereClip Between <TEntity>(Expression <Func <TEntity, object> > expression, object lo, object hi, string paramName = null) where TEntity : class { string propertyName = SqlQueryUtils.GetPropertyName(expression); return(Between(propertyName, lo, hi, paramName)); }
/// <summary> /// Apply a "greater than or equal" constraint to the named property /// </summary> /// <param name="expression">The name of the Property in the class.</param> /// <param name="value">The value for the Property.</param> public static WhereClip Ge <TEntity>(Expression <Func <TEntity, object> > expression, object value, string paramName = null) where TEntity : class { string propertyName = SqlQueryUtils.GetPropertyName(expression); return(Ge(propertyName, value, paramName)); }
/// <summary> /// 不为空 /// </summary> /// <param name="expression"></param> /// <returns></returns> public static WhereClip IsNotNull <TEntity>(Expression <Func <TEntity, object> > expression) where TEntity : class { string propertyName = SqlQueryUtils.GetPropertyName(expression); return(IsNotNull(propertyName)); }
internal OrderByClip OrderBy <TEntity>(Expression <Func <TEntity, object> > expression, SortDirection sortDirection = SortDirection.Ascending) { string propertyName = SqlQueryUtils.GetPropertyName(expression); return(OrderBy(propertyName, sortDirection)); }