在我的应用程序中,我有一些实现一个通用接口的类,我们称之为 IValidator。每个实现此类接口的类都返回 PredicateGroup 对象。因此,出于测试目的,我决定从数据库的特定视图中获取所有数据,然后在返回的集合(IEnumerable)上通过 linq where(无需使用不同谓词对数据库进行多次调用)进行快速过滤。
dapper 是否支持从 IPredicate/PredicateGroup 到 Func<> 的这种转换,或者还有其他更快/更好的解决方案?
这是我想要实现的一个小演示:
IEnumerable<Products> products = null;
using (var cn = new SqlConnection("connectionstring"))
{
//Get all elemnts from database(using only one call)
products = cn.GetList<Products>(Predicates.Field<Products>(f => f.Discontinued, Operator.Eq, true));
}
// class which implement IValidator and returns predicate group
List<IPredicate> computerPredicates = new List<IPredicate>
{
Predicates.Field<Products>(f => f.ProductName, Operator.Eq, "Computer"),
Predicates.Field<Products>(f => f.Price, Operator.Eq, 1200)
};
var computerPredicatesGroup = new PredicateGroup {Predicates = computerPredicates };
// class which implement IValidator and returns predicate group
List<IPredicate> phonePredicates = new List<IPredicate>
{
Predicates.Field<Products>(f => f.ProductName, Operator.Eq, "Phone"),
Predicates.Field<Products>(f => f.Price, Operator.Eq, 400)
};
var phonePredicatesGroup = new PredicateGroup { Predicates = phonePredicates };
var computers = products.Where( /* computerPredicates */); //??
var phones = products.Where( /* phonePredicatesGroup */); //??
相关分类