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:
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
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