Core Implementation of linq

Source: Internet
Author: User

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);

 

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.