Ну, я делал такие вещи раньше.Я надеюсь, что смогу помочь.Что вы можете сделать, это построить выражение для условия в _dbSet.Any
Примерно так:
public Expression<Func<T,bool>> GetCondition(string nameProperty, string text)
{
var i = Expression.Parameter(typeof(T), "i");
var prop = Expression.Property(i, nameProperty);
var value = Expression.Constant(text);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var containsMethodExp = Expression.Call(prop, method, value);
var lambda = Expression.Lambda<Func<T, bool>>(containsMethodExp, i);
return lambda;
}
тогда вы можете использовать его следующим образом:
private void CheckDuplicateDescription(T template)
{
var propDescription = GetPropertyNameDescription();//asuming you have the name of the Description property
var value = GetValueDescription(template, propDescription);
var condition = GetCondition(propDescription, value);
if (_dbSet.Any(condition))
{
throw new DuplicatePropertyException("Description",
string.Format(Messages.alreadyExistsWithValue, template.Descrip.Trim()));
}
}