In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Linq Lambda expressions". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Linq query statement in the era of Craft 3.0
In C # 3.0, we have new tools to improve the code.
Anonymous delegation is nice, but we want simpler, easier to maintain code. Caching 3.0 provides the concept of Linq Lambda expressions. You can use Linq Lambda expressions as a shortcut to applying anonymous delegates. Here is a query rewritten in Linq Lambda expressions:
Static IEnumerable GoldWatch (IEnumerable employees) {return Filter (employees, employee = > employee.Years > 3);} static IEnumerable SalesForce (IEnumerable employees) {return Filter (employees, employee = > employee.Department== "Sales");}
This code is fairly simple and easy to maintain, but there are still some problems.
◆ GoldWatch (employees)
◆ SalesForce (employees)
You will be aware of this problem when you see such a call. From OO's point of view, we are already familiar with the form of call noun.verb (). Ideally, we want to be able to query a collection with this syntax:
◆ employees.GoldWatch ()
◆ employees.SalesForce ()
Someone might define a new Employee class that implements IEnumerable. But the problem is that our users may want to implement it in other IEnumerable, such as Employee [] and List.
Extension method 3.0 addresses this approach with the extension method:
Static IEnumerable Filter (this IEnumerable employees, Choose choose) {foreach (Employee employee in employees) {if (choose (employee)) {yield return employee;} static IEnumerable GoldWatch (this IEnumerable employees) {return employees.Filter (employee = > employee.Years > 3);} static IEnumerable SalesForce (this IEnumerable employees) {return employees.Filter (employee = > employee.Department== "Sales");}
This looks good, but what if we query Customer like Employee? Or rather, check our inventory?
Instead of writing a separate Filter method for each class, we can write Filter as a general function:
Delegate bool Choose (T t); static IEnumerable Filter (this IEnumerable items, Choose choose) {foreach (T item in items) {if (choose (item)) {yield return item;} / / now we can filter any type we want! Int [] a = new int [] {1meme 2je 3je 4je 5}; a.Filter (I = > iTunes 1 | | iTunes 3); / / this filtering method is so useful and universal that an implementation called Where has been built into C# / / the actual Where implementation public delegate T Func (A0 arg0) shown on PDC; public static IEnumerable Where (this IEnumerable source, Func predicate) {foreach (T element in source) {if (predicate (element) yield return element) This is the end of the content of "how to use Linq Lambda expressions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.