LINQ Learning (6): orderby/group by clause

Source: Internet
Author: User

Test data:

            Class  Student {  Public   String Name { Get ; Set  ;}  Public   Int Score { Get ; Set  ;} List <Student> students = New List <student>{  New Student {name = "  Terry  " , Score = 50  },  New Student {name = "  Tom  " , Score = 85  },  New Student {name = " Wade  " , Score = 90  },  New Student {name = "  James  " , Score = 70  },  New Student {name = "  Kobe  " , Score = 90 },  New Student {name = "  AK  " , Score = 90  },}; 

 

1. orderby

Note: In the query expression,OrderbyThe clause can sort the set in ascending or descending order (Ascending by default ).You can specify multiple sort values to perform one or more secondary sort operations.

Sort scores in descending order, and then sort the names of students with the same scores in ascending order (scores are sorted in Primary Order and names are sorted in secondary order ):

             VaR Query = From Student In Students  Orderby  Student. Score descending, student. Name  Select  Student;  Foreach ( VaR Student In  Query) {console. writeline (  "  {0 }:{ 1}  "  , Student. Name, student. Score ); //  AK: 90  //  KOBE: 90  //  WADE: 90  //  Tom: 85  //  JAMES: 70  //  TERRY: 50 }

 

2. Group

(1) Description: GroupClause returnsIgrouping<Tkey, telement>Object Sequence. These objects contain zero or more items that match the Group's key values.   For example, you can group string sequences by the first letter in each string.   char , and is stored in the key property of each igrouping & lt; tkey, telement & gt; object. "> in this case, the first letter is a key and has the char type, and is stored in each igrouping tkey, in the key attribute of the telement > object.

Group by Student Score:

             VaR Query = From Student In Students Group student by student. Score;  Foreach ( VaR Studentgroup In  Query ){  //  Studentgroup is inferred as igrouping <int, student> type Console. writeline ( "  {0}  "  , Studentgroup. Key );  //  50 //  85  //  90  //  70 }

 

(2) Description:BecauseGroupThe igrouping <tkey, telement> object generated by the query is essentially a list. Therefore, you must use a nested foreach loop to access each item in each group. The External Loop is used to cycle the access group key, and the internal loop is used to cycle each item in the Access Group itself. A group may have keys but no elements. If you want to perform additional query operations on each group, you can use the into context keyword to specify a temporary identifier.   UseIntoYou must continue to write the query and useSelectStatement or anotherGroupClause ends the query.

Query the information of each student in each score group:

             VaR Query = From StudentIn  Students Group student by student. Score into G  Select  G;  Foreach ( VaR Studentgroup In  Query) {console. writeline (  "Score  GROUP: {0}  "  , Studentgroup. Key );  Foreach (VaR Student In  Studentgroup) {console. Write (  "  {0 }:{ 1 },  "  , Student. Name, student. Score);} console. writeline ();  //  GROUP: 50  //  TERRY: 50,  //  GROUP: 85  // Tom: 85,  //  GROUP: 90  //  WADE: 90, Kobe: 90, AK: 90,  //  GROUP: 70  //  JAMES: 70, }

 

(3) Same,GroupThe sub-statement can followAny TypeGroup,Such as string, built-in numeric type, user-defined naming type, or anonymous type. The format is similar. For more information, see

 

Author: forevernome
Source: http://www.cnblogs.com/ForEvErNoME/
You are welcome to repost or share it, but be sure to declare it Article Source. If the article is helpful to you, I hope you can Recommendation Or Follow

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.