I make LINQ query for the Books sample database: http://www.codeproject.com/KB/linq/linqtutorial.aspx Sorry for external link, but I don’t know how to provide database structure in the question. Currently I have this query:
var result = from book in dataContext.Books
join book_author in dataContext.BookAuthors
on book.Id equals book_author.Book
into book_authors
join category in dataContext.BookCategories
on book.Category equals category.Id
select new
{
Book = book.Id,
Title = book.Title,
Category = book.Category,
CategoryName = category.Name,
BOOK_Authors = book_authors
// , Author_Name = ???
};
This query result has BOOK_Authors subsequence: int Book, int Author. Authors database table (int Id, varchar Name) is not used in the query. I want to add author name for every member of BOOK_Authors subsequence condition: Author = Id. For example:
BOOK_Authors.Author = 1 ----- take Name form the Authors table by Id = 1 BOOK_Authors.Author = 2 ----- take Name form the Authors table by Id = 2 ...
Is this possible to do with single LINQ request?
Solution using Microsoft Entity Framework is surprisingly simple. Database wrapper classes generated by Entity Framework Wizard contain navigation fields, which allow to access all related fields without writing join queries. So, in Entity Framework project the query is:
All related fields, like category name, authors collection and each author name, are accessed using navigation fields.