从单个数据表c#

时间:2018-12-26 20:38:11

标签: c# linq lambda

我在数据库中有2个具有相同表结构的类

public class Student
{
   public int SId {get; set;}
   public string SName {get; set;}
   public List<StudentDetail> SDetail {get; set;}
}

public class StudentDetail
{
   public int StudentDetailId {get; set;}
   public int StudentId {get; set;}
   public int ExamId {get; set;}
   public string Department {get; set;}
   public string Address{get; set;}
}

通过使用StudentId联接两个表从SQL中获取数据。每个StudentId可以具有多个Student详细信息。 现在,我想填充以上两个类中的数据。我写了下面的代码。由于一个ID具有多个详细信息,这就是为什么我使用group by的原因。

    var list = dataTable.AsEnumerable()
                   .Select(x => new Student
                   {
                       SId = x.Field<int>("SId"),
                       SName = x.Field<string>("SName"),
                       SDetail = new List<StudentDetail>
                       {
                            new StudentDetail
                            {
                                StudentDetailId = x.Field<int> 
                                ("StudentDetailId"),
                                ExamId = x.Field<int>("ExamId"),
                                Department = x.Field<string>("Department") 
                            }
                       }.ToList()
                   }).GroupBy(x => x.SId).Select(x => 
                      x.FirstOrDefault()).ToList();

但是例如对于ID 1,如果有2条详细信息,则“学生详细信息”列表应具有2条记录。但是这段代码创建了两个ID记录,在“学生详细信息”中各有一个记录,如下所示

        Id -> 1 -> SId -> 1
        Id -> 1 -> SId -> 2

数据应如下创建:

        Id -> 1 -> SId -> 1
                -> SId -> 2

有帮助吗?

1 个答案:

答案 0 :(得分:2)

Select方法为每条记录创建一个new Student。对于SDetail字段,您将创建一个仅包含一个StudentDetail的列表。然后,当您按SId字段分组时,每个Student仍只包含一个StudentDetail

解决方案是先进行分组。然后,您可以考虑每个组代表一个Student,每个组的元素代表一个StudentDetail

var list = dataTable.AsEnumerable()
                    .GroupBy(rec => rec.Field<int>("SId"))
                    .Select(gr => new Student
                    {
                        SId = gr.Key,
                        SName = gr.First().Field<string>("SName"),
                        SDetail = gr.Select(detail => new StudentDetail
                        {
                            StudentDetailId = detail.Field<int>("StudentDetailId"),    
                            ExamId = detail.Field<int>("ExamId"),
                            Department = detail.Field<string>("Department")
                        }).ToList()
                     }).ToList();

GroupBy方法还有另一个重载,它结合了GroupBySelect

var list = dataTable.AsEnumerable()
                    .GroupBy(rec => rec.Field<int>("SId"),
                             (key, gr) => new Student
                             {
                                 SId = key,
                                 SName = gr.First().Field<string>("SName"),
                                 SDetail = gr.Select(detail => new StudentDetail
                                 {
                                     StudentDetailId = detail.Field<int>("StudentDetailId"),
                                     ExamId = detail.Field<int>("ExamId"),
                                     Department = detail.Field<string>("Department")
                                 }).ToList()
                              }).ToList();