The code below works but I want to optimize the code using yield or by changing algorithm.
public IEnumerable<Book> GetAuthorWithBookName(string keyword)
{
var bookAndAuthorList = new List<Book>();
List<Author> AuthorNameList = getAuthorName(keyword);
foreach (var author in AuthorNameList)
{
XmlNode booksNames = getBook(author);
XDocument XDOCbooksNames = XDocument.Parse(booksNames.OuterXml);
var bookNameList = (
from x1 in XDOCbooksNames.Descendants("Books")
select x1.Elements("book").Select(g => g.Attribute("name").Value))
.ToList();
foreach (var bookName in bookNameList)
{
bookAndAuthorList.Add(new Book()
{
authorName = author.authorName,
bookName = bookName
});
}
}
return bookAndAuthorList;
}
public class Book
{
public string authorName { get; set; }
public string bookName { get; set; }
}