通过LINQ将表格数据转换为对象

时间:2014-03-17 15:39:51

标签: linq

       StudentId    Name  AddResss
        1            A      ABC
        1            A      XYZ

如何将上述结果转换为以下对象列表

     Class Result {
      int StudentId {get;set;}
      List<String> Address {get;set;}
      }

基本上需要在studentId上分组

2 个答案:

答案 0 :(得分:3)

studentList.GroupBy(x => x.StudentId)
           .Select(x => new Result 
                        { 
                            StudentId = x.Key,
                            Address = x.Select(y => y.AddResss).ToList()
                        });

答案 1 :(得分:0)

var results =
    from row in table
    group row.Address by new { row.StudentId, row.Name } into g
    select new Result { StudentId = g.Key.StudentId, Addresses = g.ToList() };