验证某字段的唯一性

var parameter = Expression.Parameter(typeof(AttendeeTable), "t");

// 会议id
var bventLeftExpression = Expression.PropertyOrField(parameter, nameof(AttendeeTable.BventId));
var bventRightExpresion = Expression.Constant(bventId, typeof(string));
var bventCompare = Expression.Equal(bventLeftExpression, bventRightExpresion);

// 字段
var leftExpression = Expression.PropertyOrField(parameter, fieldName);
var rightExpression = Expression.Constant(value, attendeeProperty.PropertyType);
var compare = Expression.Equal(leftExpression, rightExpression);

// and关联
var expression = Expression.AndAlso(bventCompare, compare);

var realExpression = Expression.Lambda<Func<AttendeeTable, bool>>(expression, parameter);
var count = await _attendeeDataAccess.GetCountAsync(realExpression);
return count > 0;

posted @ 2019-05-09 18:04  相忘江湖何处去  阅读(338)  评论(0编辑  收藏  举报