Http://www.cnblogs.com/kongyiyun/archive/2010/10/19/1855274.html
Using a lambda expression will cause code duplication in the subject part of the lambda expression.
VarAllemployees=NewList<Employee>() {NewEmployee {EmployeeId=1, classification=1, FirstName="Skin", LastName="Sen"} };VarEarlyfolks=FromEInchAllemployeeswhereE.Monthlysalary<4000 e = = 1 & & .< Span style= "background: #22282a; Color:white; " >yearsofservice 20 select e ;
If every time we want to get a different working-class data. It is necessary to repeat it. Believe in the "high-reuse, loose coupling" of the law of you. I'm sure we'll do everything we can to achieve high reuse and loose coupling. In the era of previous method calls. Maybe you'll distill it out.
private static boolLowpaidsalaried (Employee E,int salar) { return e
.
< && e = = 1;}
In this way, every time we call, we will greatly reduce the amount of code and improve reusability.
VarEarlyfolks=Fromin allemployee where lowpaidsalaried (E, 4000) && e > 20 e;
However, it is unfortunate that. This refactoring approach reduces its reusability. In fact, the first method is more reusable than the second. Why? Obviously, it has been refined to reuse methods. This is related to the evaluation, parsing, and final execution of lambda expressions.
The previous <<LINQ expression and the mapping of the method call >> said. The compiler performs the conversion of a lambda expression to a different content based on a different LINQ provider. For LINQ to Object, it is converted to a delegate method. and LINQ To SQL is converted to the number of expressions. The data iteration is converted to SQL statement execution. So if we are refactoring in Linq2sql or ADO. The compile time passed. However, the runtime will fail. Because you cannot convert your custom method to a related SQL statement. therefore. An exception will be thrown.
Can lambda expressions just repeat and repeat? Of course not. Deferred execution is a good way to make it work. Deferred execution saves not a value, but rather a method or step to get a value. So, every time we call "get" The method of the data. Actually, the data is not available. It's just a series of "steps." We can add steps on top of the steps. This is the perfect way to achieve a lambda refactoring.
public staticIQueryable<Employee>Lowpaidsalaried (ThisIQueryable<Employee>Sequence) {Return fromSInch where s.< Span style= "background: #22282a; Color:white; " >classification == 1 && .monthlysalary 4000 select s;
Findallemployees (); allemployees. Lowpaidsalaried ();
So. Only when data is needed, the corresponding data is obtained according to the "step". For Ienumerable<t>, we can use yield return to return a sequence.
The most efficient way to take a lambda expression in a complex query is to encapsulate a query-creation extension method that encloses a generic type. The "step" is superimposed by a small method that contains a lambda expression. To achieve the most efficient optimization.
Go: "More effective C #" lambda expression optimization