C# 实体框架DbSet动态查询
我正在从传入的类型创建一个DbSet,需要查询数据库中的动态字段和值。使用泛型,我将使用带有lambda表达式的where函数。这可以通过如下创建的标准数据库集实现吗C# 实体框架DbSet动态查询,c#,linq,entity-framework,C#,Linq,Entity Framework,我正在从传入的类型创建一个DbSet,需要查询数据库中的动态字段和值。使用泛型,我将使用带有lambda表达式的where函数。这可以通过如下创建的标准数据库集实现吗 DbSet table = dataContext.Set(EntityType); PropertyInfo propertyInfo = EntityType.GetProperty(PropertyName); // Expression: "entity" ParameterExpression parameter =
DbSet table = dataContext.Set(EntityType);
PropertyInfo propertyInfo = EntityType.GetProperty(PropertyName);
// Expression: "entity"
ParameterExpression parameter = Expression.Parameter(EntityType, "entity");
// Expression: "entity.PropertyName"
MemberExpression propertyValue = Expression.MakeMemberAccess(parameter, propertyInfo);
// Expression: "value"
object convertedValue = Convert.ChangeType(value, propertyInfo.PropertyType);
ConstantExpression rhs = Expression.Constant(convertedValue);
// Expression: "entity.PropertyName == value"
BinaryExpression equal = Expression.Equal(propertyValue, rhs);
// Expression: "entity => entity.PropertyName == value"
LambdaExpression lambda = Expression.Lambda(equal, parameter);
现在需要查询表以获取数据。有一个名为'System.Linq.Dynamic'的nuget包 此软件包允许您使用字符串针对数据库集形成语句,如下所示: myContext.MyDbSet.Where(“PropertyName==@0”,“aValue”) 这是可以做到的,用你在问题中建议的表达方式,但这个库可以消除所有的负担
我在以前的一个项目中成功地使用了它。您可以尝试以下方法:
public class UniqueRecordValidationAttribute : ValidationAttribute
{
public IValidationAttribute DynamicInstance { get; private set; }
public UniqueRecordValidationAttribute(Type type,
params object[] arguments )
{
DynamicInstance =
Activator.CreateInstance(type, arguments) as IValidationAttribute;
}
public override bool IsValid(object value)
{
return DynamicInstance.IsValid(value);
}
}
public class UniqueRecordValidator<C, E, P> : IValidationAttribute
where C : DataContext, new() where E : class
{
Func<E, P, bool> Check { get; set; }
public UniqueRecordValidator(Func<E, P, bool> check)
{
Check = check;
}
public bool IsValid(object value)
{
DataContext dataContext = new C();
Table<E> table = dataContext.GetTable<E>();
return table.Count(i => Check(i as E, (P)value)) == 0;
}
}
public interface IValidationAttribute
{
bool IsValid(object value);
}
public class UniqueRecordValidator<C, E> : IValidationAttribute
where C : DataContext, new() where E : class
{
string PropertyName { get; set; }
public UniqueRecordValidator(string propertyName)
{
PropertyName = propertyName;
}
public bool IsValid(object value)
{
DataContext dataContext = new C();
Table<E> table = dataContext.GetTable<E>();
return table.Count(PropertyName + " = @0", value) == 0;
}
}
[UniqueRecordValidation(
typeof(UniqueRecordValidator<AssetTrackingEntities, ATUser_Account>)
"User_Login")]
public string User_Login { get; set; }
不确定这是否适用于
EF
。但是你可以试试动态linq库,你想用它实现什么?也许有更好的解决方案…@chrfin-这是验证属性的一部分,我正在传递属性名称和实体类型,需要查询您是否可以显示演示实体并调用?因为这听起来好像可以用不同的方法来解决……你能告诉我们你得到了什么参数吗?我假设一个类型,它只是属性和对象值的名称,还是一个表达式谢谢你的建议,但是你确定你可以使用带有属性的泛型吗??请检查:好的,我不知道。您可以使用如下内容:-将属性用作实际泛型检查的包装。
public class UniqueRecordValidator<C, E> : IValidationAttribute
where C : DataContext, new() where E : class
{
string PropertyName { get; set; }
public UniqueRecordValidator(string propertyName)
{
PropertyName = propertyName;
}
public bool IsValid(object value)
{
DataContext dataContext = new C();
Table<E> table = dataContext.GetTable<E>();
return table.Count(PropertyName + " = @0", value) == 0;
}
}
[UniqueRecordValidation(
typeof(UniqueRecordValidator<AssetTrackingEntities, ATUser_Account>)
"User_Login")]
public string User_Login { get; set; }