lundi 21 mai 2018

What is the best way to implement aggregation across nodes?

I wanted to get your opinion on how best I should aggregate results across different properties.

If I have following structure of classes in my solution (simplified version):

School object has a list of students studying and books used in the school. Each student has a list of books that they are using to study. Each book has a name and cost.

E.g.

public class School
{
    public List<Student> Students { get; set; }
    public List<Book> Books { get; set; }
    public string Area {get;set;}
    public string SchoolName {get;set;}
}

public class Student
{
    public List<Book> Books { get; set; }
    public string StudentName {get;set;}
}

public class Book
{
    public double Cost { get; set; }
    public string BookName { get; set; }
}

This structure is given to us in the 3rd party library that we need to call to.

Cost of book vary for each student.

I have following School/Student/Book information available e.g.

School:

School ABC, Area London:
   **Students**:  
        StudentA
              **Books**:  
                  Book1, Cost £1.  
                  Book 2, Cost £2 
         StudentB
              **Books**:
                  Book1, Cost £1.5.  
                  Book 2, Cost £1

 School DEF, Area NewYork  
 **Students**:  
        StudentC
              **Books**:  
                  Book1, Cost £1.  
                  Book 2, Cost £2 
         StudentD
              **Books**:
                  Book1, Cost £1.5.  
                  Book 2, Cost £1

Aim

For each area, give the total of each book across all schools and students e.g.

Area: London Book1: £5 (1+1.5+1+1.5) Book2: £6 (sum of all book 2 entries).

Note

  1. We have a books property under school so I was thinking of creating new book entries under school which aggregate all books' cost under students. 2. Group by school Area and BookName and then return sum of cost for books but I am unsure if there is another suitable way to implement it?

  2. Is there a pattern that I can use to aggregate across child nodes e.g. Books within Students and move them to Students etc.?

Aucun commentaire:

Enregistrer un commentaire