public static List<Book> BooksListByAuthor(Author author) { using (var db = new BookshelfDbContext()) { return db.Books .Where(x => x.Author.Id == author.Id) .OrderBy(x => x.Name) .ToList(); } }
public static Author AddAuthor(Author author) { if (author == null) return null; using (var db = new BookshelfDbContext()) { db.Authors.Add(author); db.SaveChanges(); var id = db.Authors.Max(x => x.Id); return db.Authors.Where(x => x.Id == id).FirstOrDefault(); } }
private void AddAuthor(object sender) { var dlg = new AddValueWindow(AddNewAuthorLabelText); if (dlg.ShowDialog() == false) return; var value = dlg.GetValue(); if (value == null || value == string.Empty) return; if (!AuthorList.Where(x => x.Name == value).Any()) { var author = new Author(); author.Name = value; using (var db = new BookshelfDbContext()) { db.Authors.Add(author); db.SaveChanges(); } OnPropertyChanged("AuthorList"); } var tAuthor = AuthorList.Where(x => x.Name == value).First(); if (tAuthor != null) AuthorID = tAuthor.Id; }