使用Lambda表达式作为参数? [英] Use Lambda expressions as a parameter?

查看:170
本文介绍了使用Lambda表达式作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用lambda表达式在我的仓库
作为一种通用的参数。如果我使用一个公司像这样的:

I would like to use the lambda expression in my Repository as a generic parameter. If I use a firm like this one:

MyEntity entity:null

void Run(Expression<Func<MyEntity ,bool>> expression)

我可以调用它是这样的:

I can call it in this way:

Run(x => x.FirstName = "Whatever")

我想是这样做的能力:

Run(x => x.FirstName = "Whatever" and x.LastName = "whatelse")

Run(x => x.FirstName = "Whatever" && x.LastName = "whatelse")

和阅读x.FirstName和x.LastName的$ b $里面的内容b中的程序跑。 ?有没有办法来实现这一目标。

And read the content of x.FirstName and x.LastName inside the procedure Run. Is there a way to accomplish that?

推荐答案

您已经声明为运行方式:

You have your Run method declared as:

void Run(Expression<Func<MyEntity ,bool>> expression)

这只会工作,如果你只是它声明为:

This would just work if you just declared it as:

void Run(Func<MyEntity ,bool> expression)

有没有你正在做一个表达的一个原因,而不仅仅是一个委托( Func键< myEntity所,布尔> )直接

Is there a reason you're taking an expression, and not just a delegate (Func<MyEntity,bool>) directly?

这篇关于使用Lambda表达式作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆