public void GetAllBooks1() { var books = new Database("product").ExecuteReader("select * from Book", null, dr => { var book = new Book {Id = (int) dr["Id"], Name = (string) dr["Name"]}; return book; }); foreach (var book in books) Console.WriteLine(book.Name); }
private static Book AddBook(string title, string description, DateTime datePublished, int pages, string isbn, int bookCopies) { using (BookDBDataContext context = new BookDBDataContext()) { Book newBook = new Book() { Title = title, BookResume = description, DatePublished = datePublished, Pages = pages, ISBN = isbn }; context.Books.InsertOnSubmit(newBook); while (bookCopies > 0) { var bookCopy = new BookCopy() { Book = newBook }; context.BookCopies.InsertOnSubmit(bookCopy); bookCopies--; } //var existing = context.Authors.FirstOrDefault(a => a.FirstName.Contains("G")); //if (existing != null) //{ // var bookAuthor = new BookAuthor() // { // BookID = newBook.ID, // AuthorID = existing.ID // }; //} //var bookGenre = new BookGenre() //{ // BookID = newBook.ID //}; context.SubmitChanges(); return newBook; } }
public static void LoanBookToUser(User user, Book book) { using (var context = new BookDBDataContext()) { bool alreadyLoaned = (from bu in context.BookUsers where bu.UserID == user.ID && bu.BookCopy.Book.ID == book.ID && bu.ReturnDate >= DateTime.Now.Date select 1).Count() > 0; if (alreadyLoaned == true) { throw new ArgumentException("You have already loaned this book!"); } BookCopy availableCopy = (from bu in context.BookUsers where bu.BookCopy.BookID == book.ID && bu.ReturnDate == null select bu.BookCopy).FirstOrDefault(); var avail = (from bc in context.BookCopies where bc.BookID == book.ID && bc.BookUsers.Count() == 0 select bc).FirstOrDefault(); if (availableCopy == null) { throw new ArgumentException("There is no available copy of this book!"); } } }
partial void DeleteBook(Book instance);
partial void UpdateBook(Book instance);
partial void InsertBook(Book instance);