猿问

分组和组合值 DataTable,c#

前:


Name    Class   Ability

Boy1      A1    Sing

Boy2      A1    Sing

Boy3      A2    Sing

Girl1     A2    Sing

Girl2     A2    Dance

后:


Name                Class   Ability

Boy1,Boy2             A1    Sing

Boy3,Girl1,Girl2      A1    Sing,Dance

如何将表格前后分组?我用过:DataTable dt = GetDataTable();//获取数据dt.AsEnumerable()....//不知道怎么继续。请帮我


至尊宝的传说
浏览 174回答 2
2回答

回首忆惘然

var result = dt.AsEnumerable()&nbsp; &nbsp; &nbsp;.GroupBy(d => d.Field<string>("Class"))&nbsp; &nbsp; &nbsp;.Select(g => new&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Name = string.Join(",",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.Select(gn => gn.Field<string>("Name")).Distinct()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Class = g.Key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Teacher = string.Join(",",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.Select(gt => gt.Field<string>("Ability")).Distinct())&nbsp; &nbsp; &nbsp;});注意:如果您直接使用 Linq 而不是 DataTable,这会容易得多。

凤凰求蛊

我为样本创建了一个虚拟数据&nbsp; &nbsp; &nbsp; &nbsp; DataTable dtNew, dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Id", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Category",typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Type",typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; dtNew = dt.Clone();&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323021", "Doors", "900");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323022", "Doors", "900");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323023", "Doors", "1000");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323024", "Doors", "1000");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323025", "Walls", "200");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323026", "Walls", "200");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323027", "Walls", "200");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323028", "Walls", "200");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323026", "Columns", "300x300");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323027", "Columns", "300x300");&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add("323028", "Columns", "500x500");此场景的解决方案&nbsp; &nbsp; &nbsp; &nbsp; //Case 1: Category and Type&nbsp; &nbsp; &nbsp; &nbsp; var caretoryTypeResult = (from b in dt.AsEnumerable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group b by new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Category = b.Field<string>("Category"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type = b.Field<string>("Type")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; into grpCategoryType&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grpCategoryType.Key.Category,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grpCategoryType.Key.Type,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grpCategoryType&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp; &nbsp; &nbsp; &nbsp; caretoryTypeResult.ForEach(list => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var category = list.grpCategoryType.AsEnumerable().Select(m => m.Field<string>("Id")).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtNew.Rows.Add(string.Join(",", category), list.Category, list.Type);&nbsp; &nbsp; &nbsp; &nbsp; });希望这段代码有帮助
随时随地看视频慕课网APP
我要回答