how to use Linq count with group by in C#

Viewed 1023

i have a datatable in which look like this
datatable input
basically i want it to be group by column featurename(distinct) in which it should sum in effort and complete column
specify all the featureid,featurename comma seprated
count of the featureid
assigned to comman seprated
now i want datatable to be look like this
enter image description here
don't know how to use the count code

var result5= dtTaskandBugs.AsEnumerable().GroupBy(x => x["Functional Area"])
                               .Select(item => new
                               {
                                   FunctionalArea = item.Key,

                                   Completedsum = item.Sum(y => Convert.ToDecimal(y["Completed"])),
                                   effortsum = item.Sum(z => Convert.ToDecimal(z["effort"])),
                                   storyids = string.Join(",", item.Select(a => a["Storyid"]).Distinct()),
                                   storiesename= string.Join(",", item.Select(b => b["StoryName"]).Distinct()),
                                   Featureid = string.Join(",", item.Select(c => c["Featureid"]).Distinct()),
                                   Featurename= string.Join(",", item.Select(d => d["FeatureName"]).Distinct()),

                               });
2 Answers

With Count() you will get count of grouped value. Try with Linq like this:

from s in dtTaskandBugs.AsEnumerable()
                        group s by s.Field<string>("Functional Area")
                            into grp
                            orderby grp.Key
                            select new { 
                                   FunctionalArea = grp.Key,
                                   Completedsum = grp.Sum(y => Convert.ToDecimal(y["Completed"])),
                                   effortsum = grp.Sum(z => Convert.ToDecimal(z["effort"])),
                                   storyids = string.Join(",", grp.Select(a => a["Storyid"]).Distinct()),
                                   storiesename= string.Join(",", grp.Select(b => b["StoryName"]).Distinct()),
                                   Featureid = string.Join(",", grp.Select(c => c["Featureid"]).Distinct()),
                                   Featurename= string.Join(",", grp.Select(d => d["FeatureName"]).Distinct()),
                                   Count = grp.Count()          // <----------------
 };

Just write item.count() and you will get count of grouped values,

Related