Dar*_*ren 5 c# lambda expression-trees linq-to-sql
我正在使用http://www.albahari.com/nutshell/predicatebuilder.aspx中的 Albaharis PredicateBuilder来过滤 Linq-to-SQL 应用程序中的结果。这一直很有效。
我现在想做的是重用现有的过滤谓词表达式来过滤将现有过滤对象作为属性的对象。
例如,我有 2 个课程,Order
并且Customer
. 我已经有一个返回 a 的方法Expression<Func<Customer, bool>>
,它是使用上面提到的谓词构建器构建的。我现在想在我的Order
过滤方法中重用它,它将Expression<Func<Customer, bool>>
通过某种方式将Order.Customer
属性(表达式?)传递到我的Customer
过滤方法中返回 a 。
我有这样的东西(远未完成,但我希望你明白):
public class CustomerSearchCriteria
{
public Expression<Func<Customer, bool>> FilterPredicate()
{
// Start with predicate to include everything
var result = PredicateBuilder.True<Customer>();
// Build predicate from criteria
if (!String.IsNullOrEmpty(this.Name))
{
result = result.And(c => SqlMethods.Like(c.Name, this.Name));
}
// etc. etc. etc
}
public class OrderSearchCriteria
{
public Expression<Func<Order, bool>> FilterPredicate()
{
// Start with predicate to include everything
var result = PredicateBuilder.True<Order>();
// Build predicate from criteria
if (!String.IsNullOrEmpty(this.Reference))
{
result = result.And(o => SqlMethods.Like(o.Reference, this.Reference));
}
// etc. etc. etc
// This is where I would like to do something like:
// result = result.And(o => o.Customer "matches" this.CustomerCriteria.FilterPredicate()
}
Run Code Online (Sandbox Code Playgroud)
任何 Linq 表达式大师可以帮助我吗?
提前致谢。
如果您使用 Albaharis 的LinqKit,您应该能够执行以下操作:
var customerFilter = this.CustomerCriteria.FilterPredicate();
// create an expression that shows us invoking the filter on o.Customer
Expression<Func<Order, bool>> customerOrderFilter =
o => customerFilter.Invoke(o.Customer);
// "Expand" the expression: this creates a new expression tree
// where the "Invoke" is replaced by the actual predicate.
result = result.And(customerOrderFilter.Expand())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
814 次 |
最近记录: |