Anshu (7) [Avatar] Offline
#1
Can someone help me?
In Chapter 4 -Book Linq in Action
I wanted to find all the authors for all books and wanted my display like that
Book | Publisher | Authors
I am facing difficulty displaying the Authors of a book and no example is shown in the book to diaply authors related to a book.
Since a Book has multiple Authors I am havign hard time to figure out what the LINQ sql will be?

Please help.
I am a beginner in LINQ
Any help is really appreciated
fabrice.marguerie (224) [Avatar] Offline
#2
Re: Books And Authors Linq Query
Hello Anshu,

I'm not sure that I understand your question.

Firstly, in chapter 4, LINQ to SQL is not used. Only LINQ to Objects is used, against the sample data in the SampleData class.
Secondly, the Book class has an Authors property that returns a book's authors. Isn't that what you want?

If you're talking about the LinqBooksDataContext from later chapters in the book, you'll find a BookAuthors property on the Book class.

Fabrice
Anshu (7) [Avatar] Offline
#3
Re: Books And Authors Linq Query
Yes I am talking about LINQ to Objects
How can I extract all the Authors of all the Books by using LINQ from SampleData.cs class.
Since in the SampleData.cs every book has Authors which is an Array I am not sure how to extract the array of Authors since every book has one or two authors.
I can see the Publisher property but not the Authors.
So is there a way I can extract the output by using LINQ in Objects( by using JOIN, grouping etc.)
I see in Chapter 4 How to extract Authors by using SELECTMANY but I want to display Books details with all the Authors.
Something like this

BOOK | PUBLISHER | AUTHORS
Funny Stories Fun Books Jonny Good, Graziella Simplegame
LINQ rules Joe Publishing Octavio Prince
C# on Rails Joe Publishing Octavio Prince
Bonjour Mon FunBooks Jonny Good ,Graziella Simplegame
etc.....

Or something like that
BOOK | PUBLISHER | AUTHORS
Funny Stories Fun Books Jonny Good
Graziella Simplegame
LINQ rules Joe Publishing Octavio Prince
C# on Rails Joe Publishing Octavio Prince
Bonjour Mon FunBooks Jonny Good
Graziella Simplegame

etc.

Thanks for such a quick reply.

Thanks for such a quick reply.
fabrice.marguerie (224) [Avatar] Offline
#4
Re: Books And Authors Linq Query
As I wrote, all you have to do is to use the Book.Authors property. There's nothing more that you need.
Is the following query what you want? For each Author in book.Authors, we select the name, and then join the names with commas.

[pre]from book in SampleData.Books
select new {
Book = book.Title,
Publisher = book.Publisher.Name,
Authors = String.Join(", ", book.Authors.Select(a => a.FirstName + " " + a.LastName).ToArray())
}[/pre]
Anshu (7) [Avatar] Offline
#5
Re: Books And Authors Linq Query
Yes thanks that's what I wanted.
Thanks so much for your help.
I have anotherr question from Chapter 4 (FormBooks.cs) which I am not able to figure out.
I run this query it shows two columns on the Form (Title, Price)
var query =from book in SampleData.Books
where book.Title.Length > 10
orderby book.Price
select new { Book = book.Title, book.Price };

When I try to convert this into Lambda Expression I see an extra column Book with Empty values
var query = SampleData.Books
.Where (book => book.Title.Length > 10)
.OrderBy (book => book.Price)
.Select (book => new { book.Title, book.Price });
Is something wrong with my query my second query operators.
Why I see three columns(Book- Empty Column, Title, price) instead of two(Title, Price)/
I should see two columns instead of three can't figure out where is the error in my second query.

Thanks once again for the quick reply.
I like the book very much.
fabrice.marguerie (224) [Avatar] Offline
#6
Re: Books And Authors Linq Query
Would the Book column be defined at the DataGrid level?
If it's the case, you just have to remove it.
Anshu (7) [Avatar] Offline
#7
Re: Books And Authors Linq Query
Thanks that was the issue.
Another question. How do I make sure that SQLEXPRESS is running on my desktop.
I am not able to connect to lia.mdf
It gives me error.
Provider/Sql Server interfaces, error -26 Error locating Server/Instance Specified
but I am able to connect in VS through Server Explorer by using the lia.mdf file
but when I execute the program in Chapter 6 it gives me the same error.

Thanks for helping me out.
Anshu (7) [Avatar] Offline
#8
Re: Books And Authors Linq Query
I am able to connect to Lia.mdf through SQL Server 2005 but not through Sql Server express and I am not sure if it is installed or not.
In order to connect through SQL Server 2005 what should be the connection string in other words what changes needs to be made in the connectionstring and in the project Ch6-8
Thanks
fabrice.marguerie (224) [Avatar] Offline
#9
Re: Books And Authors Linq Query
If SQL Server works for you instead of SQL Server Express, then you're better off using SQL Server.

If you have a connection in Server Explorer, you can get the connection string from the connection properties.
In order to build other connection strings, you can get help at http://www.connectionstrings.com
Anshu (7) [Avatar] Offline
#10
Re: Books And Authors Linq Query
Thanks, I have another question
In Chapter 7 , I dont understand this code
How are we rolling back a transaction whereas I thought if we context.SubmitChanges();
the changes are submitted to the database
I dont see any command for rollback a transaction in the code.
I thought the changes will be visible in the database but the data is same in the database. I am unable to figure out how it is rolling back a transaction.

Please explain.

DisconnectedTest_7_6b()
using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
{
UpdateAuthor(cachedAuthor.ID, cachedAuthor.FirstName, "Testing change", cachedAuthor.WebSite, cachedAuthor.TimeStamp);
//refetch it and show the database value now
ObjectDumper.Write(from a in context1.Authors where a.ID == cachedAuthor.ID select a);

//Rollback the change after running the demo
}

UpdateAuthor(Guid id, String firstName, String lastName, String webSite, Byte[] timeStamp)
LinqBooksDataContext context = new LinqBooksDataContext();
context.Log = Console.Out;
context.Authors.Attach(new Author
{
ID = id,
FirstName = firstName,
LastName = lastName,
WebSite = webSite,
TimeStamp = timeStamp
}, true);

context.SubmitChanges();
fabrice.marguerie (224) [Avatar] Offline
#11
Re: Books And Authors Linq Query
A transaction is committed only if TransactionScope.Complete() is called.
In the code you show, this call is missing, so an implicit Rollback is performed.

If SubmitChanges() is called in the context of a transaction, everything it does is rolled back if that transaction is not committed.

Please, use a different thread for each question, with a different subject, so that everyone can easily see your questions and the answers. Thanks.
Anshu (7) [Avatar] Offline
#12
Re: Books And Authors Linq Query
Thanks Fabrice for clearing my doubts on transaction.
I will make sure that I post my questions on a separate thread.