foreach (var book in query)
{
Response.Write(book.Title);
foreach (var author in book.Authors)
{
Response.Write(" , "+author.FirstName +" "+ author.MiddleInitial +
" "+ author.LastName );
}
}
Right now I am printing the Book Title : All author names with a “,”.
I would like print : (If they have more than one author separate each author with a “,” (done) and add “AND” to the last author name – If they don’t have more than one author no “, is required.
How can I do that??
You would be better of building the string first, and then writing it to the screen outside of the loop. I would suggest using a
StringBuilderfor this.To use the
StringBuilderclass, you’ll need to import theSystem.Textnamespace:And here is some sample code to demonstrate: