Exemplo n.º 1
0
        /// <summary>
        /// 获得Where表达式
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private static string GetBatchConditionQuery <TEntity>(Expression <Func <TEntity, bool> > predicate, List <SqlParameter> parameters)
            where TEntity : class
        {
            ConditionBuilder conditionBuilder = new ConditionBuilder();

            conditionBuilder.Build(predicate.Body);
            if (!string.IsNullOrEmpty(conditionBuilder.Condition))
            {
                string condition = conditionBuilder.Condition;
                for (int i = 0; i < conditionBuilder.Arguments.Length; i++)
                {
                    condition = condition.Replace(string.Format("{{{0}}}", i), string.Format("@p{0}", parameters.Count));
                    parameters.Add(new SqlParameter(string.Format("@p{0}", parameters.Count), conditionBuilder.Arguments[i]));
                }
                return("Where " + condition);
            }
            return(null);
        }
Exemplo n.º 2
0
        public int Delete <T>(Expression <Func <T, bool> > predicate) where T : class
        {
            string tableName = AttributesHelper.GetTableName <T>();

            if (!string.IsNullOrEmpty(tableName))
            {
                string           command          = String.Format("DELETE FROM {0}", tableName);
                ConditionBuilder conditionBuilder = new ConditionBuilder();
                conditionBuilder.Build(predicate.Body);
                if (!String.IsNullOrEmpty(conditionBuilder.Condition))
                {
                    command += " WHERE " + conditionBuilder.Condition;
                }

                return(this.Database.ExecuteSqlCommand(command, conditionBuilder.Arguments));
            }

            return(0);
        }