以下のコードは機能しますが、yield を使用するか、アルゴリズムを変更してコードを最適化したいです。
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; }
}