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 optimize Lambda expression in C #

2025-02-24 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 optimize Lambda expressions in C#". Many people will encounter this dilemma in the operation of actual cases, 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!

Using Lambda expressions will cause code duplication in the topic part of the Lambda expression.

Var allEmployees = new List () {new Employee {EmployeeId = 1, Classification = 1, FirstName = "Skin", LastName = "Sen"}}; var earlyFolks = from e in allEmployees where e.MonthlySalary

< 4000 && e.Classification == 1 && e.YearsOfService >

20 select e

If we want to get the data of different working class every time. You have to repeat it. You who believe in the law of "high reuse and loose coupling". We will certainly try our best to achieve high reuse and loose coupling. In the old days of method invocation. Maybe you'll extract it.

Private static bool LowPaidSalaried (Employee e, int salar) {return e.MonthlySalary

< salar && e.Classification == 1; } 这样,每次我们调用的时候,将大大减少代码量,提高可复用性. var earlyFolks = from e in allEmployee where LowPaidSalaried(e, 4000) && e.YearsOfService >

20 select e

Unfortunately, however. Here。 This way of refactoring reduces its reusability. In fact, the reusability of the method is higher than that of the second method. Why? Obviously, the reuse method has been extracted. This is related to the evaluation, parsing and final execution of Lambda expressions.

It was said in the previous one. The compiler converts Lambda expressions into different contents according to different LINQ Provider. For LINQ to Object. Will be converted to a delegated method. LINQ to SQL is converted to an expression number. It is converted to SQL statement execution only when the data is iterated. So。 If we are refactoring like this in LINQ2SQL or ADO.Net EF. The compilation period passed. But there will be an error at run time. Because your custom method cannot be converted into related SQL statements, so. An exception will be thrown.

Do Lambda expressions have to be repeated over and over again? Of course not。 Here。 Delaying execution is a good way to play its role. As I said earlier. Delaying the execution of the save is not the value, but the method or step of getting the value. In this way, every time we finish calling the method to get the data. Actually。 The data are not yet available. I got it. It's just a series of steps. We can add steps on top of the steps. Like this。 The refactoring under Lambda is realized.

Public static IQueryable LowPaidSalaried (this IQueryable sequence) {return from s in sequence where s.Classification = = 1 & & s.MonthlySalary < 4000 select s;} var allEmployees = FindAllEmployees (); var salaried = allEmployees.LowPaidSalaried ()

Like this。 Only when the data is needed, the corresponding data will be obtained according to the steps. For IEnumerable, we can use yield return to return the sequence.

The most effective way to use Lambda expressions in complex queries is to encapsulate query creation and extension methods of closed generic types. Overlay the steps by a small method that contains Lambda expressions. In order to achieve the most effective optimization.

So much for the content of "how to optimize Lambda expressions in C #". 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.

Share To

Development

Wechat

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

12
Report