And then, we often mention two things in the last article (LINQ Series: Basics and Nature (part I)), System.Linq.Enumerable and System.Func. So what exactly are two things? Let's look at the following code:
When a Linq expression is compiled, the compiler converts the query operator to a call (or other type) for several functions in the System.Linq.Enumerable type. Here are a few enumerable member functions:
Code
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
System.Func<TSource,int,bool> predicate)
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
System.Func<TSource,bool> predicate)
public static int? Sum( this IEnumerable<int?> source );
We can see that most of these functions require proxies as arguments, and in particular, generic proxy func<> defined in System.Core.dll, a large part of which is a new feature of c#3.x: Extension method. Interested friends can go and have a look.
Let's look at the definition of func<>: The last type parameter TResult represents the return type:
Code
1 public delegate TResult Func<T0,T1,T2,T3,TResult>(
2 T0 arg0, T1 arg1, T2 arg2, T3 arg3)
3 public delegate TResult Func<T0,T1,T2,TResult>(T0 arg0, T1 arg1, T2 arg2)
4 public delegate TResult Func<T0,T1,TResult>(T0 arg0, T1 arg1)
5 public delegate TResult Func<T0,TResult>(T0 arg0)
6 public delegate TResult Func<TResult>()
Because many of the member functions of System.Linq.Enumerable require proxies as arguments, we can implement them in the following ways when invoked:
Define a query expression using the query operator
This is the most straightforward and concise way to refer to the example code for the previous article (LINQ Series: Basics and Nature (part I)).
Use enumerable type functions and lambda expressions to define query expressions
Refer to the example code for the previous article (LINQ Series: Fundamentals and Nature (part I)).