Lambda expressions are more advanced forms evolved from anonymous methods. For more information about the anonymous method, see http://msdn.microsoft.com/msdnmag/issues/04/05/c20 /. For the evolution of lambda expressions, see http://msdn.microsoft.com/msdnmag/issues/07/06/csharp30/default.aspx? Loc = Zh. The original English version is http://msdn.microsoft.com/msdnmag/issues/07/06/csharp30 /.
1. Lifting in lambda expressions
In C #2.0, the use of anonymous methods is like this. Class someclass
{
Delegate void somedelegate ();
Public void invokemethod ()
{
Somedelegate del = delegate ()
{
MessageBox. Show ("hello ");
};
Del ();
}
}
In the article about the evolution of LINQ and its impact on the design of C #, it asserted that if Lambda expressions are first introduced into the language, there will be no need for anonymous methods. Lambda expressions are also mentioned in some previous articles in this series, but they are not quoted in depth. This article is about lifting in lambda expressions. We will start to understand some details about lambda expressions.
Compile the following small program and check whether the output result is stunned by using system;
Using system. Collections. Generic;
Using system. LINQ;
Namespace Tester
{
Class Program
{
Static void main (string [] ARGs)
{
List <func <int> List = new list <func <int> ();
For (INT I = 0; I <3; I ++)
{
List. Add () => I );
}
Foreach (VAR item in List)
{
Console. writeline (item ());
}
}
}
}
We define a list, which is stored in the format of func <int>, that is, an int-type proxy is returned. Then, use the for loop to encapsulate I into the lambda expression and add it to the list. Then, use foreach to output the result cyclically. The essence of lambda expressions is a proxy, which points to an anonymous function. Therefore, we use item () to call it and execute the function.
The problem is that what you expect to output is 0, 1, 2, and the actual result is 3. Why? This involves two reasons.
First, there can be only one I variable in the for loop. That is to say, the address of I is allocated during the first loop, and will not be changed because of the number of cycles. The only change is the value loaded in the loop.
Second, when constructing a Lambda expression, the variable address is passed in, not the specific value. The value of the lambda expression is determined only when the lambda expression is actually executed. This is why in the above example, the result is 3. (At the end of the for loop, I was added with 1)
We can easily solve the problem. In the for loop, define a temporary variable and store the I value. The compiler will re-allocate the memory for this temporary variable. In this way, the new memory will be re-allocated for each loop. Run the following example. Using system;
Using system. Collections. Generic;
Using system. LINQ;
Namespace enterprisetester
{
Class Program
{
Static void main (string [] ARGs)
{
List <func <int> List = new list <func <int> ();
For (INT I = 0; I <3; I ++)
{
Int temp = I;
List. Add () => temp );
}
Foreach (VAR item in List)
{
Console. writeline (item ());
}
}
}
}
Does it meet your requirements? This temp is called lifting. Lift is an elevator in American language, which is translated as a ladder or stepping stone.
2. The impact of lifting on LINQ to SQL.
Lambda expressions are used in a large number of applications in LINQ to SQL. This problem will inevitably affect the formation of its SQL statements. See the following example string [] KEYWORDS = string [] KEYWORDS = new string [] {"111", "222", "333", "444 "};
Somedatacontext CTX = new somedatacontext ();
VaR entitys = from E in CTX. Entity
Select E;
Foreach (string keyword in keywords)
{
Entitys = entitys. Where (E => E. Text. Contains (keyword ));
}
VaR q = entitys. tolist ();
The intention is to search for all records that meet fuzzy match. In the actual generated SQL statement, only "% 444%" is input ". Add lifting, and the problem will be solved.
You can use the for loop to replace the foreach loop. If lifting is not added, will it throw an exception? Study the cause by yourself.