public List <Author_popularity> FoundAuthorPopularity(List <Book> foundBooks) { //Author Popularity: //----------------------------------- //get authors of selected works: List <String> FoundAuthors = foundBooks.ToList().Select(book => book.AuthorName).ToList(); //foreach author: foreach (var author in FoundAuthors) { //Author_popularityTable Table from db: List <Author_popularity> popularAuthors = db.Author_popularityTable.ToList(); Boolean authorExists = false; foreach (var TableAuthor in popularAuthors) { if (TableAuthor.AuthorName == author) { //update author: Author_popularity alteredAuthor = (from x in db.Author_popularityTable where x.AuthorName == author select x).First(); alteredAuthor.Popularity += 1; db.SaveChanges(); //Set the flag if the author exists authorExists = true; } } //if the author isn't exists if (!authorExists) { //create author: Author_popularity createdAuthor = new Author_popularity(author, 1); db.Author_popularityTable.Add(createdAuthor); db.SaveChanges(); } //if db.Author_popularityTable is empty: if (db.Author_popularityTable.ToList().Count == 0) { //create author: Author_popularity createdAuthor = new Author_popularity(author, 1); db.Author_popularityTable.Add(createdAuthor); db.SaveChanges(); } } //----------------------------------- //Author_popularityTable Table from db: List <Author_popularity> resultPopularAuthors = db.Author_popularityTable.ToList(); return(resultPopularAuthors); }
public void AddBook(Book book) { //book.Author.Books.Add(); db.Books.Add(book); db.SaveChanges(); }