-1

I am using System.Linq.Dynamic to group columns using linq dynamically and need to perform

How can I SUM a column i.e.(Sum(Deposits)). User will select table and columns at runtime. enter image description here Ex: The above table has Territory, State, BankName, Year, QuarterFromMainT

Assume user has selected these columns for grouping (Territory, State, BankName) and Now wants to sum a Column ex:Deposits. How can I do this? Because only ASEnumerable and AsQueryable only has sum. We need to perform groupby and sum

Click here for further clarification and My Code is:

enter image description here

here t1 is table and qfields is a list which contains grouping columns.

How can use linq to Sum a column after groupby?

Community
  • 1
  • 1
Ranjith kumar
  • 223
  • 2
  • 6
  • 11
  • Possible duplicate? http://stackoverflow.com/questions/61870/sum-of-items-in-a-collection – user1477388 Aug 07 '13 at 17:05
  • Of course this is not my business, but I feel it's right thing to warn you and ask: did you post sensitive data? If yes - your employer/customer won't be happy. – alex.b Aug 07 '13 at 18:40

2 Answers2

0

Do you mean this? If not, please disregard...

var query = rows
    .GroupBy(row => new { row.Territory, row.State, row.BankName })
    .Select(group => new
    {
        group.Key.Territory, group.Key.State, group.Key.BankName,
        Deposits = group.Sum(row => row.Deposits)
    });
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
0

You need to Set Footer Text to display your sum amount

Example:

 <asp:BoundField DataField="Deposits" 
             FooterText="Total Desposits" />

and in Gridview you need to write coding like this

 Gridview.Columns[4].FooterText = dt.AsEnumerable().Select(x => x.Field<int>("Desposits")).Sum().ToString();

        Gridview.DataSource = dt;

        Gridview.DataBind();

its working in my application. you can use it according to your convenient.

Dineshcool
  • 39
  • 2
  • 14