C#LINQ Group Summary

Source: Internet
Author: User

There are two types of LINQ groupings, one is table grouping summary and the other is list<> grouping totals. The following examples illustrate two groups of subtotals.

first, group totals for tables:

DataTable table = new DataTable ();
Table. Columns.Add ("Column name 1", Type.GetType ("System.Int32"));
Table. Columns.Add ("Column Name 2", Type.GetType ("System.Int32"));
Table. Columns.Add ("Column name 3", Type.GetType ("System.String"));
Table. Columns.Add ("Column name 4", Type.GetType ("System.String"));
Table. Columns.Add ("Column name 5", Type.GetType ("System.String"));
Table. Columns.Add ("Value or Amount column name", Type.GetType ("System.String"));

Assigning Values to table tables

......

GROUP by [column name 2]

var dtsummary = from Q in table. AsEnumerable ()
Group Q by new {column name 2= q.field<int> ("Column Name 2")} into G
Select New
{
Column name 2= g.key. Column Name 2,
Value or amount summary column name = G.sum (a => a.field<decimal> ("Value or Amount column name")
};

The second type of,list<> group rollup:

var listsummary = from P in list< entity >
Group p by new {Property 1 = P. Property 1} into G
Select New
{
Property 1 = G.key. Property 1,
Sum of value or amount = G.sum (a => a. numeric or amount attribute)
};


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.