INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

Linq sort a list inside another list

Linq sort a list inside another list

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close