A summary of the LINQ expressions

Source: Internet
Author: User

All the operations of LINQ are based on the operation of the iteratable object. The simplest is, of course, array processing.

// First example: the simplest example

String [] STRs = {"zhangran", "zhangxiaoran", "xiaozhangran", "ranxiaozhang", "ranzhangxiao", "xiaoxiaoran "}; vaR Index = from N in STRs where n. startswith ("Z") // Where is the limit operator select N; // select this is a required foreach (string I in index) {console. writeline (I);} console. readkey (); // The variants in the preceding example use methods to query string [] STRs = {"zhangran", "zhangxiaoran", "xiaozhangran", "ranxiaozhang ", "ranzhangxiao", "xiaoxiaoran"}; var Index = STRs. where (n => N. startswith ("Z"); foreach (string I in index) {console. writeline (I );}

Console. readkey ();

// Example 2: sort search results/* string [] STRs = {"zhangran", "zangxiaoran", "zixiaozhangran", "ranxiaozhang", "ranzhangxiao ", "xiaoxiaoran"}; var Index = from N in STRs where n. startswith ("Z") orderby N // other options: orderby descending and orderby n. substring (N. length-1) Select N; foreach (string I in index) {console. writeline (I);} console. readkey (); * // The preceding variant: sort by method/* string [] STRs = {"zhangran", "zangxiaoran", "zixiaozhangran", "ranxiaozhang ", "ranzhangxiao", "xiaoxiaoran"}; // var Index = STRs. orderby (n => N ). where (n => N. startswith ("Z"); // var Index = STRs. orderbydescending (n => N ). where (n => N. startswith ("Z"); var Index = STRs. orderby (n => N. substring (N. length-1 )). where (n => N. startswith ("Z"); foreach (string I in index) {console. writeline (I);} console. readkey ();

*/

 

// Use of aggregate Operators

/* Int [] ints = {1, 2, 4, 5, 6, 7, 8, 9}; var Index = from N in ints where n> 1 select N; // The foreach traversal console is no longer used here. writeline (index. count (); console. writeline (index. average (); console. writeline (index. min (); console. writeline (index. max (); console. writeline (index. sum (n => n + 1); console. readkey ();*/

 

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.