使用Lambda表达式作为参数?

发布于 2024-08-06 07:19:44 字数 525 浏览 6 评论 0 原文

我想在我的存储库中使用 lambda 表达式 作为通用参数。如果我使用这样的公司:

MyEntity entity:null

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

我可以这样称呼它:

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 的内容 程序运行。有办法实现吗?

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")

What I would like is the ability to do something like this:

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

or

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

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

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

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

发布评论

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

评论(4

时光瘦了 2024-08-13 07:19:44

您将 Run 方法声明为:

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

如果您只是将其声明为:

void Run(Func<MyEntity ,bool> expression)

是否有理由采用表达式,而不仅仅是委托 (Func)直接地?

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)

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

在风中等你 2024-08-13 07:19:44

我认为你需要使用

void Run(Action<MyEntity> action)

then

Run(x => {x.FirstName = "Whatever"; x.LastName = "whatelse"})

因为我没有看到你使用返回值。

不确定表达式> 是否有效会起作用(如果你需要检查它),但你可以尝试。

I think you need to use

void Run(Action<MyEntity> action)

and then

Run(x => {x.FirstName = "Whatever"; x.LastName = "whatelse"})

since I don't see that you use return value.

Not sure if Expression<Action<MyEntity>> will work (if you need to inspect it) but you can try.

爱情眠于流年 2024-08-13 07:19:44

总之,OP 传入 Expression> 参数而不是 Func 参数的原因是他/她想在 Run 方法中检查表达式本身。

Expression 对象具有多个属性,使您能够在运行时检查表达式的特性。其中之一“主体”可用于识别表达式中使用的代码,而“参数”集合可用于检查编译时可传递给表达式的参数的类型和顺序。

Console.WriteLine(expression.Parameters[0].Type); //writes MyEntity
Console.WriteLine(expression.Body); 
   //writes ((x.FirstName == "Bob" && x.LastName == "Smith"))

在您的示例中,您正在定义一个表达式,但实际上尚未调用它。因此,您实际上并未传入参数(MyEntity 对象),因此没有任何参数可供检查。

All, the reason why the OP is passing in an Expression<Func<MyEntity, bool>> parameter as opposed to a Func<MyEntity, bool> parameter is that he/she wants to inspect the expression itself within the Run method.

The Expression object has several properties that enable you to inspect the peculiars of the expression at runtime. One of these, Body, can be used to discern the code used within the expression, while the Parameters collection can be used to inspect the type and order of parameters that can be passed to the expression when compiled.

Console.WriteLine(expression.Parameters[0].Type); //writes MyEntity
Console.WriteLine(expression.Body); 
   //writes ((x.FirstName == "Bob" && x.LastName == "Smith"))

In your example, you are defining an expression but you haven't actually called it. Therefore, you haven't actually passed in a parameter (the MyEntity object) and so there aren't any arguments to inspect.

山田美奈子 2024-08-13 07:19:44

您是否尝试过以下操作:

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

Have you tried the following:

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