使用Linq中的分组从数据集中填充对象

时间:2015-01-31 13:49:28

标签: c# linq

我正在尝试使用Linq将数据从datatable加载到对象。以下是我的情景。我有下表结构和数据:

seq name    id  class
1   Rajesh  101 B
1   kumar   102 B
1   sandeep 104 A
2   Myur    105 B
2   Bhuvan  106 C
3   Siraz   107 A

以下是我的班级结构

public class student
{
    public string name {get;set;}
    public string id { get; set; }
    public string meritClass { get; set; }
}

public class stdGroup
{
    public int seqId{get;set;}
    public List<student> students;
}

作为最终输出,我应该为每个seq构造一个Student。应该stdGroup [三个对象]创建seq对象。

示例:

stdGroup对象1将包含3个学生对象

stdGroup对象2将包含2个学生对象

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

这应该做你需要的(假设你用DataTable表示DataTable):

List<stdGroup> stdGroups = myDataTable
    .AsEnumerable()
    .GroupBy(a =>  a.Field<int>("Seq"), a => new student() { id = a.Field<string>("Id"), name = a.Field<string>("name"), meritClass = a.Field<string>("class") })
    .Select(a => new stdGroup() { seqId = a.Key, students = a.ToList() })
    .ToList();

要将其分解,首先,将数据表行置于可以使用linq的状态,

.AsEnumerable()

现在,进行分组 - 选择seq作为组的密钥,并为每个条目构建一个学生对象,该对象将被分配到相应的组。

.GroupBy(a =>  a.Field<int>("Seq"), a => new student() { id = a.Field<string>("Id"), name = a.Field<string>("name"), meritClass = a.Field<string>("class") })

现在,为每个组创建stdGroup对象,并从我们的组密钥填充seq属性,并获取每个组的内容,并将其分配给students属性

.Select(a => new stdGroup() { seqId = a.Key, students = a.ToList() })    

最后,并且可选地,转换为列表而不是可枚举。

.ToList();

答案 1 :(得分:0)

您还可以查看我的实施:

public class dbStudent
{
    public int seq;
    public string name;
    public int id;
    public string meritClass;
}

public class student
{
    public string name { get; set; }
    public int id { get; set; }
    public string meritClass { get; set; }
}

public class stdGroup
{
    public int seqId { get; set; }
    public List<student> students { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var dbStudebts = new List<dbStudent>();
        dbStudebts.Add(new dbStudent { seq = 1, name = "Rajesh", id = 101, meritClass = "B" });
        dbStudebts.Add(new dbStudent { seq = 1, name = "kumar", id = 102, meritClass = "B" });
        dbStudebts.Add(new dbStudent { seq = 1, name = "sandeep", id = 104, meritClass = "A" });
        dbStudebts.Add(new dbStudent { seq = 2, name = "Myur", id = 105, meritClass = "B" });
        dbStudebts.Add(new dbStudent { seq = 2, name = "Bhuvan", id = 106, meritClass = "C" });
        dbStudebts.Add(new dbStudent { seq = 3, name = "Siraz", id = 107, meritClass = "A" });

        var result = (from o in dbStudebts
                      group o by new { o.seq } into grouped
                      select new stdGroup()
                      {
                          seqId = grouped.Key.seq,
                          students = grouped.Select(c => new student()
                          {
                              name = c.name,
                              id = c.id,
                              meritClass = c.meritClass
                          }).ToList()
                      }).ToList();
    }
}