使用 NET Core 和 EF Core 构建谓词 [英] Predicate build with NET Core and EF Core
本文介绍了使用 NET Core 和 EF Core 构建谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
非常简单的问题:
我正在尝试创建一个这样的谓词构建器:
I'm trying to create a predicate builder like this :
var predicate = PredicateBuilder.False<MyObject>();
但似乎在 Net Core 和 EF Core 中不可用.
But seems that is not available in Net Core And EF Core.
我是不是丢了包裹什么的?
Am I missing a package or something ?
推荐答案
您确定您使用的 PredicateBuilder
不是自定义类吗?PredicateBuilder
作为 LINQKit 的一部分提供,但
Are you sure that the PredicateBuilder
you were using was not a custom class? A PredicateBuilder
is shipped as part of LINQKit but the source is also available here as follows:
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
return Expression.Lambda<Func<T, bool>>
(Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
}
}
这篇关于使用 NET Core 和 EF Core 构建谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文