将 LambaExpression 添加到 IQueryable 的实例

发布于 2024-08-25 05:58:31 字数 566 浏览 4 评论 0 原文

 ParameterExpression parameter = Expression.Parameter(typeof(Product), "x");
        MemberExpression Left = Expression.MakeMemberAccess(parameter, typeof(Product).GetProperty("Name"));
        ConstantExpression Right = Expression.Constant(value, typeof(String));
        BinaryExpression expression = Expression.Equal(Left, Right);
        LambdaExpression lambada = Expression.Lambda<Func<Product, bool>>(expression, parameter);

现在,如何将这个 lambda 添加到 IQuerybale 的实例中,比如说 _query

_query.Where(lambada.Compile());?

 ParameterExpression parameter = Expression.Parameter(typeof(Product), "x");
        MemberExpression Left = Expression.MakeMemberAccess(parameter, typeof(Product).GetProperty("Name"));
        ConstantExpression Right = Expression.Constant(value, typeof(String));
        BinaryExpression expression = Expression.Equal(Left, Right);
        LambdaExpression lambada = Expression.Lambda<Func<Product, bool>>(expression, parameter);

Now how do I add this lambada to an instance of an IQuerybale, lets say _query

_query.Where(lambada.Compile());?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2024-09-01 05:58:31

我认为你只需要更改 lambda 的类型

ParameterExpression parameter = Expression.Parameter(typeof(Product), "x");
MemberExpression Left = Expression.MakeMemberAccess(parameter, typeof(Product).GetProperty("Name"));
ConstantExpression Right = Expression.Constant(value, typeof(String));
BinaryExpression expression = Expression.Equal(Left, Right);
Expression< Func< Product, bool > > lambda = Expression.Lambda<Func<Product, bool>>(expression, parameter);

现在它是一个 Expression> 和 IQueryable.Where以此作为论点。 Expression.Lambda 返回一个 TDelegate,它也是一个 LambdaExpression,这就是 Expression.Lambda 行编译的原因在你的情况和我的情况下,但 IQueryable.Where 希望将其视为 Expression>

像这样的东西:

List< Product > products = new List< Product >
{
    new Product { Name = "bar" }, 
    new Product { Name = "foo" }
};
IQueryable< Product > queryable = products.AsQueryable().Where( lambda );

I think you just need to change the type of lambda

ParameterExpression parameter = Expression.Parameter(typeof(Product), "x");
MemberExpression Left = Expression.MakeMemberAccess(parameter, typeof(Product).GetProperty("Name"));
ConstantExpression Right = Expression.Constant(value, typeof(String));
BinaryExpression expression = Expression.Equal(Left, Right);
Expression< Func< Product, bool > > lambda = Expression.Lambda<Func<Product, bool>>(expression, parameter);

Now it is an Expression<Func<Product, bool>> and IQueryable.Where takes that as an argument. Expression.Lambda<TDelegate> returns a TDelegate that is also a LambdaExpression which is why Expression.Lambda line compiles in your case and in my case, but IQueryable.Where wants to see it as an Expression<Func<Product, bool>>.

Something like:

List< Product > products = new List< Product >
{
    new Product { Name = "bar" }, 
    new Product { Name = "foo" }
};
IQueryable< Product > queryable = products.AsQueryable().Where( lambda );
慵挽 2024-09-01 05:58:31

不要使用 .Compile,它会将表达式转换为委托。 IQueryable 使用表达式而不是委托进行过滤:

_query = _query.Where(lambada);

Don't use .Compile, it would transform the expression into a delegate. An IQueryable is filtered using an expression, not a delegate :

_query = _query.Where(lambada);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文