Q:

linq-to-entities join 3 tables and group-by statement

Consider the database schema in the picture.

I need a linq-to-entities statement that will retrieve authors and their respective books grouped by author. For example, I want to display something as follows:

authorName8
  bookTitle27
  bookTitle35
  bookTitle62
authorName37
  bookTitle9
  bookTitle51

An additional constraint is the oject of grouped items has to be returnable by a method.
Can anybody formulate join and group-by statement (a) ordered by author name, (b) ordered by author name and book title?

A:

You didn't mention which version of EF you are using, or whether you need to filter the authors by some condition, or filter the books by some condition. A general query that will fetch everything will look like this:
List<Author> authorsWithBooks = 
                 context.
                 Authors.
                 Include(a => a.Books). // For EF 4.1
                 /*Include("Books"). instead for previous versions of EF */
                 /*Where(a => !a.IsDeleted). if you want to filter the authors */
                 ToList();

If you want to filter the book collections, you will have to write a more elaborate query though.

