Reputation:
I need help in turning this linq expression into a linq statment. SampleData.Publishers and SampleData.Books are simple collections that I have from the Linq in Action book.
Here is the expression
var pubBooks =
from pub in SampleData.Publishers
join book in SampleData.Books on pub.Name equals book.Publisher.Name into pubbks
select new {
Publisher = pub.Name,
Books =
from b in pubbks
select b.Title
};
Here is what I have so far, but I can't seem to get the books collection defined in the anonymous type. Thanks for your time.
var pubBooks = SampleData.Publishers.Join(SampleData.Books, pub => pub.Name, book => book.Publisher.Name, (pub, book) => new {
Publisher=pub.Name,
Books=??????
});
Upvotes: 2
Views: 133
Reputation: 5156
Resharper gave me this:
var pubBooks =
SampleData.Publishers.GroupJoin(
SampleData.Books,
pub => pub.Name,
book => book.Publisher.Name,
(pub, pubbks) => new
{
Publisher = pub.Name,
Books =
from b in pubbks
select b.Title
});
Upvotes: 0
Reputation: 48255
You can use GroupJoin
.
I didn't test this but it could look like this:
var pubBooks = SampleData.Publishers.GroupJoin(SampleData.Books, pub => pub.Name, book => book.Publisher.Name, (pub, bookColl) => new {
Publisher = pub.Name,
Books = bookColl.Select(b => b.Title)
});
Upvotes: 2