c#datatable LAMDA Expressions and LINQ statements using the GroupBy method

Source: Internet
Author: User

Https://www.cnblogs.com/johnblogs/p/6006867.html

DataTable ds = new DataTable ();

1.LAMDA expression (recommended)
var result = ds. AsEnumerable (). GroupBy (s = = New{year = S.field<int> ("year"), month = s.field<int> ("month"), day = S.field<int> (" Day ")});

2. Linq syntax The final compiler converts it to LAMDA expression
var result = from S in DS. Tables[0]. AsEnumerable ()
Group S by new {year = S.field<int> (' year '), month = s.field<int> ("month"), day = s.field<int> ("Day")} Into temp

Select temp;

DataTable using the GroupBy method needs to be aware that result is of type igrouping<int,datarow>
foreach (var thisgroup in result)
{
foreach (var row in Thisgroup)
{
Traverse all the row currently in this group
}
}

Statistics do not repeat the number, no test is not known whether the DT can be used. Defaultview.totable (True, new string[1] {"UserName"}).   Rows.Count; Http://www.xuebuyuan.com/1990057.htmlusing (DataTable dt = ds. Tables[0])
{
Three variables record the total number of records, the number of users that are not duplicated, the total amount of all users
int Rowscount, Distinctuserrowscount, Allusermoney;
Rowscount = dt. Rows.Count;
Distinctuserrowscount = dt. Defaultview.totable (True, new string[1] {"UserName"}). Rows.Count;
Allusermoney = Convert.todecimal (Dt.compute ("sum (Usermoney)", ""));
}

c#datatable LAMDA Expressions and LINQ statements using the GroupBy method

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.