"More effective C #" lambda expression optimization

Source: Internet
Author: User
Keywords expressions steps we execute nbsp;
Tags code data different expression it is list return select

Using a lambda expression will cause code duplication in the topic portion of the lambda expression.

1. var allemployees = new List<employee> () {
2. New Employee {
3. EmployeeId = 1, classification = 1, FirstName = "Skin", LastName = "Sen"}};
4. var earlyfolks = from E in allemployees
5. Where E.monthlysalary < 4000 && e.classification = 1 && e.yearsofservice > 20
6. Select E;

If every time we want to get a different class of data. It's time to repeat it. Believe in the "high reuse, loose coupling" of the law of you. You'll do whatever it takes to achieve high reuse and loose coupling. In the era of previous method calls. Maybe you'll refine it.

1. private static bool Lowpaidsalaried (Employee e, int salar)
2. {
3. Return E.monthlysalary < Salar && e.classification = 1;
4.}

In this way, every time we call, it will greatly reduce the amount of code, improve the reusability of

var earlyfolks = from E in Allemployee
where Lowpaidsalaried (e, 4000) && E.yearsofservice > 20
Select E;

Unfortunately, however, it is here. This refactoring approach reduces its reusability. In fact, the first method is more reusable than the second. Why? It is clear that the reuse method has been refined. This is related to the evaluation of lambda expressions, parsing, and how they are ultimately executed.

The previous <<linq expression and the method call mapping >>. The compiler executes the lambda expression into different content based on different LINQ provider. For LINQ to Object. will be converted to a delegate method. and LINQ To SQL is converted to the number of expressions. is converted to SQL statement execution when the data is iterated. So, if we were so refactored in the Linq2sql or Ado.net EF. The compilation period passed. But the runtime will fail. Because you cannot convert your custom method to a related SQL statement. So. An exception will be thrown.

Is it possible for lambda expressions to be repeated again? No, it's here. Deferred execution well, it's a good place to play its role. Deferred execution saves not the value, but the method or step that gets the value. Method of data. Actually, the data is not yet available. It's just a series of "steps." We can add the steps on the basis of the steps. So, it's perfect for refactoring under the lambda.

1. public static iqueryable<employee>
Lowpaidsalaried (this iqueryable<employee> sequence)
2. {
3. Return from S in sequence
4. Where s.classification = = 1 && s.monthlysalary < 4000
5. Select S;
6.}var allemployees = Findallemployees ();
7. var salaried = allemployees.lowpaidsalaried ();

In this way, the data will be obtained according to the "steps" only when data is needed. For Ienumerable<t>, we can return the sequence using yield returns.

The most efficient way to take a lambda expression in a complex query is to encapsulate the query creation extension method of the enclosing generic type. Add "Steps" by using a small method that contains a lambda expression. To achieve the most effective optimization.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.