简体   繁体   English

如何在C#中使用Linq计数与group by

[英]how to use Linq count with group by in C#

i have a datatable in which look like this 我有一个数据表,看起来像这样
数据输入
basically i want it to be group by column featurename(distinct) in which it should sum in effort and complete column 基本上我希望它是按列featurename(distinct)分组,它应该在努力和完整列中加总
specify all the featureid,featurename comma seprated 指定所有featureid,featurename逗号分隔
count of the featureid featureid的计数
assigned to comman seprated 分配给comman seprated
now i want datatable to be look like this 现在我希望数据表看起来像这样
在此输入图像描述
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()),

                               });

With Count() you will get count of grouped value. 使用Count()您将获得分组值的计数。 Try with Linq like this: 尝试使用这样的Linq

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()          // <----------------
 };

只需编写item.count() ,您将获得分组值的计数,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM