Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linq sort a list inside another list

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
US
I have a list of books that I have grouped, summed and ordered the groups. But I can't figure out how to order the books within the groups.

I have the following:
Code:
   public class Book
    {
        public string Title;
        public string Author;
        public string Genre;
        public decimal Price;
    }
   public class GroupCountSum<K, T, C, D>
   {
      public K Key;
      public IEnumerable<T> Values;
      public int Count;
      public decimal Summed;
   }
      public void GroupAndSum()
      {
         public List<GroupCountSum<string, Book, int, decimal>> BooksCountedSummed;
         public List<Book> Books = new List<Book>();

          // Add test data
         Books.Add(new Book { Author = "Douglas Adams", Title = "The Hitchhiker's Guide to the Galaxy", Genre = "Fiction", Price = 159.95M });
         Books.Add(new Book { Author = "Scott Adams", Title = "The Dilbert Principle", Genre = "Fiction", Price = 23.95M });
         Books.Add(new Book { Author = "Douglas Coupland", Title = "Generation X", Genre = "Fiction", Price = 300.00M });
         Books.Add(new Book { Author = "Walter Isaacson", Title = "Steve Jobs", Genre = "Biography", Price = 219.25M });
         Books.Add(new Book { Author = "Michael Freeman", Title = "The Photographer's Eye", Genre = "Photography", Price = 195.50M });

        //Group the books by Genre count number of books and sum the prices
         var booksGroupedCountedSummed = (from b in Books
                                         group b by b.Genre into g
                                         select new GroupCountSum<string, Book, int, decimal> 
                                         {   
                                            Key = g.Key, 
                                            Values = g, 
                                            Count = g.Count(), 
                                            Summed = g.Sum(x => x.Price) 
                                         }).OrderBy(g=>g.Key);

         BooksCountedSummed = booksGroupedCountedSummed.ToList();

       }

This will group the books by genre, get a count and sum the group as well as order the groups by genre.

But the book titles are out of order.

How would I order the books inside the groups?

Thanks,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top