Query operators and extension methods
The query operator is an important facility of linq, which uses extended methods to define query operations;
For example, the where OPERATOR:
Initial linq expression
Core Implementation of Linq
1 public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) 2 { 3 foreach (T item in source) 4 { 5 if (predicate(item)) 6 { 7 return (IEnumerable<T>)item; 8 } 9 }10 return null;11 }
Initial linq expression
1 class app 2 { 3 static void main() 4 { 5 string[] names={"Burke","Connor","Frank","Everett","Albert","George","Harris","David"}; 6 7 IEnumerable<string> query=from s in names 8 where s.Length==5 9 orderby s10 select s.ToUpper();11 12 13 foreach(string item in query)14 {15 Console.WriteLine(item);16 }17 18 }19 }
Lambda expressions, similar to the following delegate to a common method to call the Extension Method
1 IEnumerable<string> query=Enumerable.Where(names,s=>s.Length<6);
C # Allow this method to call the Extension Method
1 IEnumerable<string> query=names.Where(s=>s.Length>6);