Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Lamda expressions Expression> and Func

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use Lamda expressions Expression and Func. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In doing C # is to use Lamda expression will save a lot of code, recently because when doing Mongodb things, want to package a C# database operation class, but for the query in the where has not considered how to deal with, inadvertently Baidu to Expression and Func, found that is really good

Description

Func is delegate

Expression is an expression

When Expression is compiled, it becomes delegate before it can run.

For example:

Expression ex = x = > x

< 100; Func func = ex.Compile(); 然后你就可以调用func: func(5) //-返回 true func(200) //- 返回 false 而表达式是不能直接调用的。 测试例子 还是用我们上次Mongodb的Demo例子 我们原先的Save按钮里写了where后面直接是lamda表达式.现在我们改成Expression的方式 在Expression per,把Tobject改为CPersonal类 per就直接可以找到对应的参数进行设置,然后在下面的where子句里面直接传入per即可 接下来我们看看运行效果 先查询所有的数据

As you can see, there are two Li Si: 42 and wilson002: 26 for those over 21 years old.

After we click Update Save

Re-query

You can see Li Si: 44 wilson002:28

We can use this parameter to pass the where query when we do the MongoDb wrapper class later.

Thank you for reading! This is the end of the article on "how to use Lamda expression Expression and Func". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report