protected void ValidateDuplicateData(DuplicatedInfo <P> checkCondition, P entity) { if (checkCondition != null && checkCondition.Groups.Count > 0) { //生成基础Query var baseExp = EntityList.AsQueryable(); var modelType = typeof(P); ParameterExpression para = Expression.Parameter(modelType, "tm"); //循环所有重复字段组 foreach (var group in checkCondition.Groups) { List <Expression> conditions = new List <Expression>(); //生成一个表达式,类似于 x=>x.Id != id,这是为了当修改数据时验证重复性的时候,排除当前正在修改的数据 MemberExpression idLeft = Expression.Property(para, "Id"); ConstantExpression idRight = Expression.Constant(entity.ID); BinaryExpression idNotEqual = Expression.NotEqual(idLeft, idRight); conditions.Add(idNotEqual); List <PropertyInfo> props = new List <PropertyInfo>(); //在每个组中循环所有字段 foreach (var field in group.Fields) { Expression exp = field.GetExpression(entity, para); if (exp != null) { conditions.Add(exp); } //将字段名保存,为后面生成错误信息作准备 props.AddRange(field.GetProperties()); } int count = 0; if (conditions.Count > 1) { //循环添加条件并生成Where语句 Expression conExp = conditions[0]; for (int i = 1; i < conditions.Count; i++) { conExp = Expression.And(conExp, conditions[i]); } MethodCallExpression whereCallExpression = Expression.Call( typeof(Queryable), "Where", new Type[] { modelType }, baseExp.Expression, Expression.Lambda <Func <P, bool> >(conExp, new ParameterExpression[] { para })); var result = baseExp.Provider.CreateQuery(whereCallExpression); foreach (var res in result) { count++; } } if (count > 0) { //循环拼接所有字段名 string AllName = ""; foreach (var prop in props) { string name = PropertyHelper.GetPropertyDisplayName(prop); AllName += name + ","; } if (AllName.EndsWith(",")) { AllName = AllName.Remove(AllName.Length - 1); } //如果只有一个字段重复,则拼接形成 xxx字段重复 这种提示 if (props.Count == 1) { ErrorListVM.EntityList.Add(new ErrorMessage { Message = AllName + "数据重复", Index = entity.ExcelIndex }); } //如果多个字段重复,则拼接形成 xx,yy,zz组合字段重复 这种提示 else if (props.Count > 1) { ErrorListVM.EntityList.Add(new ErrorMessage { Message = AllName + "组合字段重复", Index = entity.ExcelIndex }); } } } } }
/// <summary> /// 验证重复数据 /// </summary> protected void ValidateDuplicateData() { //获取设定的重复字段信息 var checkCondition = SetDuplicatedCheck(); if (checkCondition != null && checkCondition.Groups.Count > 0) { //生成基础Query var baseExp = DC.Set <TModel>().AsQueryable(); var modelType = typeof(TModel); ParameterExpression para = Expression.Parameter(modelType, "tm"); //循环所有重复字段组 foreach (var group in checkCondition.Groups) { List <Expression> conditions = new List <Expression>(); //生成一个表达式,类似于 x=>x.Id != id,这是为了当修改数据时验证重复性的时候,排除当前正在修改的数据 var idproperty = typeof(TModel).GetProperties().Where(x => x.Name.ToLower() == "id").FirstOrDefault(); MemberExpression idLeft = Expression.Property(para, idproperty); ConstantExpression idRight = Expression.Constant(Entity.GetID()); BinaryExpression idNotEqual = Expression.NotEqual(idLeft, idRight); conditions.Add(idNotEqual); List <PropertyInfo> props = new List <PropertyInfo>(); //在每个组中循环所有字段 foreach (var field in group.Fields) { Expression exp = field.GetExpression(Entity, para); if (exp != null) { conditions.Add(exp); } //将字段名保存,为后面生成错误信息作准备 props.AddRange(field.GetProperties()); } //如果要求判断id不重复,则去掉id不相等的判断,加入id相等的判断 if (props.Any(x => x.Name.ToLower() == "id")) { conditions.RemoveAt(0); BinaryExpression idEqual = Expression.Equal(idLeft, idRight); conditions.Insert(0, idEqual); } int count = 0; if (conditions.Count > 1) { //循环添加条件并生成Where语句 Expression conExp = conditions[0]; for (int i = 1; i < conditions.Count; i++) { conExp = Expression.And(conExp, conditions[i]); } MethodCallExpression whereCallExpression = Expression.Call( typeof(Queryable), "Where", new Type[] { modelType }, baseExp.Expression, Expression.Lambda <Func <TModel, bool> >(conExp, new ParameterExpression[] { para })); var result = baseExp.Provider.CreateQuery(whereCallExpression); foreach (var res in result) { count++; } } if (count > 0) { //循环拼接所有字段名 string AllName = ""; foreach (var prop in props) { string name = PropertyHelper.GetPropertyDisplayName(prop); AllName += name + ","; } if (AllName.EndsWith(",")) { AllName = AllName.Remove(AllName.Length - 1); } //如果只有一个字段重复,则拼接形成 xxx字段重复 这种提示 if (props.Count == 1) { MSD.AddModelError(GetValidationFieldName(props[0])[0], Program._localizer["DuplicateError", AllName]); } //如果多个字段重复,则拼接形成 xx,yy,zz组合字段重复 这种提示 else if (props.Count > 1) { MSD.AddModelError(GetValidationFieldName(props.First())[0], Program._localizer["DuplicateGroupError", AllName]); } } } } }
/// <summary> /// 获取列头内容 /// </summary> /// <returns>Html内容</returns> protected virtual string GetHeader() { string rv = PropertyHelper.GetPropertyDisplayName(PI); return rv == null ? "" : rv; }