(Turn) C # LINQ and LAMDA expression application experience GroupBy grouping

Source: Internet
Author: User

This article was reproduced from: http://www.cnblogs.com/han1982/p/4138163.html

Example 1:

Application of GroupBy grouping in list<> generics

Original table:

Results by name Nam Group:

Lamda queries on a DataTable must be added in a reference to the project System.Data.DataSetExtensions

Code:

 Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } List<Person> persons1 =NewList<person>(); Private voidForm1_Load (Objectsender, EventArgs e)          {initform (); }          Private voidinitform () {//form Initializationpersons1. ADD (NewPerson ("Zhang San","male", -, the)); Persons1. ADD (NewPerson ("Wang Cheng","male", +,3200)); Persons1. ADD (NewPerson ("Lili","female", +,1700)); Persons1. ADD (NewPerson ("Joint","female", *,3600)); Persons1. ADD (NewPerson ("Joint","female", -, the)); Datagridview1.datasource=persons1; }                Private voidButton1_Click (Objectsender, EventArgs e) {              //******* the collection by name belongs to group GroupBy query ********//fields included in the results://1. Group keyword: Name = G.key//2. Number of each group: Count = G.count ()//3. The sum of the ages of each grouping: AGEC = g.sum (item + Item). Age)//4. Sum of revenue for each grouping: MONEYC = g.sum (item + Item). Money)//notation 1:LAMDA expression (recommended)            varls = persons1. GroupBy (A = a.name). Select (g = (New{name = G.key, Count = G.count (), AGEC = g.sum (item = Item). Age), MONEYC = g.sum (item =item.              (Money)})); //notation 2: Class SQL language The final compiler translates it into a LAMDA expression            varLS2 = fromPsinchPERSONS1 Group PS by PS. Name into GSelect New{name = G.key, Count = G.count (), AGEC = g.sum (item = Item). Age), MONEYC = g.sum (item =item.                    Money)}; Datagridview1.datasource=ls.             ToList (); //Datagridview1.datasource = ls2.  ToList ();         }      }            /// <summary>      ///manually design a person class. For placing in a list generic/// </summary>       Public classPerson { Public stringName {Get;Set; }  Public intAge {Get;Private Set; }  Public stringSex {Get;Set; }  Public intMoney {Get;Set; }  PublicPerson (stringNamestringSexintAgeintMoney ) {Name=name; Age=Age ; Sex=sex; Money=Money ; }      }

(Turn) C # LINQ and LAMDA expression application experience GroupBy grouping

Related Article

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.