简体   繁体   中英

Convert the reader to the list of class

I have a class

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

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

My stored proc will return something like this

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

I would like to represent this as student Class

I am able to group by the studentID but not able to get books 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;

}

page is not a collection, it is just an integer. Each student has a list of books each book has an Id and a page number property.

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

Class Book
{
Int  bookId,
Int numberOfPages
}

You can do this with the help of 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();

Working Demo

Update :

Since the question is updated to say pages received in , separated form, you can modify the query to

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();

Please note, you can even simplify this answer if you say studentId, bookId combination is unique.

Updated Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM