Click here to Skip to main content
16,010,918 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an Excel with below rows and columns,

A B C
1 Name Type Value
2 x1 a 2
3 x2 b 1
4 x3 a 1
5 x2 b 2
6 x2 b 1
7 x1 a 6and I want to group "type" and sum the "value" and FYI : I wanted to achieve below results

Name x1 x2 x3
Type
a 8 0 1
b 0 4 0


Same Question :http://stackoverflow.com/questions/26091458/group-by-and-sum-with-linqtoexcel[^]
Posted
Updated 28-Sep-14 19:16pm
v2
Comments
BillWoodruff 29-Sep-14 2:10am    
What have you tried so far: show your code.
Member 11053420 2-Oct-14 2:27am    
Hi, i have tried below code .. i wanted to group and sum the values but its not grouping at all. Pls find my code below
var groupp=from k in excelFile.Worksheet(sheetName).ToList() select k;
var grouped = from row in groupp group row by row["Issue Type"] into g select new { edd = g.Key, c = g.Sum(row => Convert.ToDouble(row["Time Spent"])) };
foreach (var groupe in grouped)
MessageBox.Show("key is" + groupe.edd + " : count=" + groupe.c);
ZurdoDev 29-Sep-14 8:55am    
Where are you stuck?
Member 11053420 7-Oct-14 0:30am    
the messagebox.show displaying as same as in excel sheet, its displaying row by row value... its not summing and grouping

1 solution

I strongly recommend you to download this tutorial: 101 LINQ Samples[^] and read this article: Using LINQ to Query Tables in Excel 2007[^] then you'll be able to create proper linq statement. Try!
 
Share this answer
 
Comments
Member 11053420 1-Oct-14 7:24am    
Thanks.. I will try and come back if required or incase if i face any issue
Member 11053420 2-Oct-14 2:28am    
Hi, i have tried below code .. i wanted to group and sum the values but its not grouping at all. Pls find my code below
var groupp=from k in excelFile.Worksheet(sheetName).ToList() select k;
var grouped = from row in groupp group row by row["Issue Type"] into g select new { edd = g.Key, c = g.Sum(row => Convert.ToDouble(row["Time Spent"])) };
foreach (var groupe in grouped)
MessageBox.Show("key is" + groupe.edd + " : count=" + groupe.c);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900