本文介绍了NET Core和EF Core的谓词构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个非常简单的问题:
我正在尝试创建这样的谓词构建器:
I'm trying to create a predicate builder like this :
var predicate = PredicateBuilder.False<MyObject>();
但是 Net Core 和 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的谓词构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!