组列表到动态行的数据表

时间:2018-04-18 16:23:29

标签: c# .net

我有一个如下列表:

public static List<Record> initRecords() {
    var list = new List<Record>();
    list.Add(new Record {UserID = "aaa", FieldName = "First Name", FieldValue = "Joey"});
    list.Add(new Record {UserID = "aaa", FieldName = "Last Name", FieldValue = "Williams"});
    list.Add(new Record {UserID = "aaa", FieldName = "Company", FieldValue = "Microsoft"});
    list.Add(new Record {UserID = "bbbb", FieldName = "First Name", FieldValue = "Larry"});
    list.Add(new Record {UserID = "bbbb", FieldName = "Last Name", FieldValue = "Johnson"});
    list.Add(new Record {UserID = "bbbb", FieldName = "Company", FieldValue = "Knicks"});
    list.Add(new Record {UserID = "bbbb", FieldName = "Comment", FieldValue = "4-1"});
    list.Add(new Record {UserID = "ccc", FieldName = "First Name", FieldValue = "Allen"});
    list.Add(new Record {UserID = "ccc", FieldName = "Last Name", FieldValue = "Houston"});
    list.Add(new Record {UserID = "ccc", FieldName = "Company", FieldValue = "Lala"});

    return list;
}

我使用LINQ将它们按用户ID分组到三个组,如您所见,每个组的记录数不同

var records = list.GroupBy(u => u.UserID).Select(grp => grp.ToList()).ToList();

现在一切都很好,但是我需要一些指导来将这个分组列表构建到数据表中,所以我可以将它转换为html表,如下所示,tks

- 插件: 也许我没有清楚地解释清楚,列表是动态列表,分组列表可以有很多&#34; FieldName&#34; 我尝试做的是将列表与UserID分组,然后选择一个列表,其中包含最大数量&#34; FieldName&#34;,将它们转换为&#34; DataTable Columns&#34;然后将每个分组列表记录循环到DataTable.NewRow

类似这样的事情

名字|姓氏|公司|评论

乔伊威廉姆斯微软

然后我可以将它循环到html表

0 个答案:

没有答案