예제 #1
0
        public virtual bool Modify(TEntity entity, ISpecification <TEntity> specification)
        {
            if (this.OnModify != null)
            {
                OnModify(ref entity, ref specification);
            }
            DBBuilder condition;

            if (specification.Lambda != null)
            {
                var ids = this.Entities.Where(specification.Lambda).Select(d => d.ID).ToList();
                if (ids.Count > 0)
                {
                    condition = DBBuilder.Define().Field(_entityDefinition.Value.TableAttribute.TableName, "ID").In(builder => builder.Value(ids.ToArray()));
                }
                else
                {
                    condition = new FalseSpec <TEntity>().Sql;
                }
            }
            else
            {
                condition = specification.Sql;
            }
            var fieldAndValues = GetFieldAndValue(entity, true, false);
            var dbBuilder      = condition.IsCommand ? condition : DBBuilder.Update(_entityDefinition.Value.TableAttribute.TableName, fieldAndValues).Where(condition);
            var result         = this.UnitOfWork.Execute(dbBuilder);

            return(result > 0);
        }
예제 #2
0
 public FieldSpec(string propertyName, IEnumerable <TProperty> propertyValues)
 {
     if (propertyValues == null || !propertyValues.Any())
     {
         var spec = new FalseSpec <TEntity>();
         Lambda = spec.Lambda;
         Sql    = new FalseSpec <TEntity>().Sql;
     }
     else if (propertyValues.Count() == 1)
     {
         var prop   = typeof(TEntity).GetProperty(propertyName);
         var entity = Expression.Parameter(typeof(TEntity), "entity");
         var left   = Expression.Constant(propertyValues.First());
         var right  = IsNullableType(prop.PropertyType) ? Expression.Property(Expression.Property(entity, prop), "Value") : Expression.Property(entity, prop);
         var exp    = Expression.Equal(left, right);
         Lambda = Expression.Lambda <Func <TEntity, bool> >(exp, entity);
         //Sql = DBBuilder.Define().Field(DataEntityUtils.Entity(typeof(TEntity)).TableAttribute.TableName, propertyName).Eq().Value(propertyValues.First());
         Sql = DBBuilder.Define().Field(null, propertyName).Eq().Value(propertyValues.First());
     }
     else
     {
         var prop   = typeof(TEntity).GetProperty(propertyName);
         var entity = Expression.Parameter(typeof(TEntity), "entity");
         var left   = Expression.Constant(propertyValues.ToList());
         var right  = IsNullableType(prop.PropertyType) ? Expression.Property(Expression.Property(entity, prop), "Value") : Expression.Property(entity, prop);
         var exp    = Expression.Call(left, "Contains", null, right);
         Lambda = Expression.Lambda <Func <TEntity, bool> >(exp, entity);
         //Sql = DBBuilder.Define().Field(DataEntityUtils.Entity(typeof(TEntity)).TableAttribute.TableName, propertyName).In(d => d.Value(propertyValues));
         Sql = DBBuilder.Define().Field(null, propertyName).In(d => d.Value(propertyValues));
     }
 }