将阅读器转换为类列表

时间:2016-02-11 06:07:10

标签: c# sqldatareader

我有一个班级

Class Student
{
    Int studentId,
    List<ClassB> Books 
}

Class Book
{
    Int  bookId,
    List<int> pages
}

我的存储过程会返回类似这样的内容

StudentId         bookId.      Page
1                         101.            500
1.                        102.            600
2.                        101.            400,500
2                         103.            500,600,700

我想将此表示为学生班级

我可以按学生ID分组,但无法获得书籍     static void Main(string [] args)         {

        var res = GetStudents();
        var result = res.AsEnumerable()
    .GroupBy(r => new { r.StudentId,r.IsPassed })
    .Select(c =>
            new Student()
            {
                IsPassed = c.Key.IsPassed,
                StudentId = c.Key.StudentId,

            }).ToList();



    }

private static List<Student> GetStudents()
{
    string connStr = ConfigurationManager.AppSettings["Conn"];
    List<Student> students = new List<Student>();
    SqlConnection conn = new SqlConnection(connStr);
    conn.Open();

    SqlCommand command = new SqlCommand("Select * from Students", conn);
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            var StudentId = Convert.ToInt32(reader["StudentId"]);
            var BookId = Convert.ToInt32(reader["BookId"]);
            var IsPassed = Convert.ToBoolean(reader["IsPassed"]);
            var PageNUmbers = reader["PageNumber"].ToString().Split(',').Select(t => Convert.ToInt32(t)).ToList();
            List<Book> books = new List<Book>();
            books.Add(new Book() { BookId = BookId, PageNumber = PageNUmbers });
            students.Add(new Student() { StudentId = StudentId, Books = books, IsPassed = IsPassed });
        }
    }
    return students;

}

2 个答案:

答案 0 :(得分:0)

页面不是一个集合,它只是一个整数。每个学生都有一本书,每本书都有一个Id和一个页码属性。

Class Student
{
Int studentId,
List<Book> Books 
}

Class Book
{
Int  bookId,
Int numberOfPages
}

答案 1 :(得分:0)

您可以在Linq的帮助下完成此操作。

DataTable dt = new DataTable();
dt.Load(reader);

var result = dt.AsEnumerable()
    .GroupBy(r=>r.Field<int>("StudentId"))
    .Select(c=> 
            new Student() 
            {
                studentId = c.Key, 
                Books = c.GroupBy(s=>s.Field<int>("bookId"))
                    .Select(b=> 
                            new Book() 
                            {
                                bookId = b.Key, 
                                pages = b.Select(s=>s.Field<int>("Page")).ToList() 
                            }).ToList()
            }).ToList();

工作Demo

更新:

由于问题已更新为以,分隔的形式收到的网页,因此您可以将查询修改为

var result = dt.AsEnumerable()
            .GroupBy(r=>r.Field<int>("StudentId"))
            .Select(c=> 
                    new Student() 
                    {
                        studentId = c.Key, 
                        Books = c.GroupBy(s=>s.Field<int>("bookId"))
                            .Select(b=> 
                                    new Book() 
                                    {
                                        bookId = b.Key, 
                                        pages = b.SelectMany(p=>p.Field<string>("Page").Split(',').Select(int.Parse)).ToList() 
                                    }).ToList()
                    }).ToList();

请注意,如果您说studentId, bookId组合是唯一的,您甚至可以简化此答案。

更新了Demo