public static void DeleteItem(IBookCatalogItem itemToDelete) { BookCatalog bookCatalog = new BookCatalog( ); DeleteItem(bookCatalog, itemToDelete); bookCatalog.SubmitChanges( ); }
// Listed in the order they appear in the tutorial public static void RetrieveYourDataWithLinqExamples( ) { BookCatalog bookCatalog = new BookCatalog( ); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("ALL BOOKS"); Console.WriteLine("*********"); foreach (Book book in bookCatalog.Books) { string title = book.Title; decimal price = book.Price; Console.WriteLine(title + ": $" + price); } IEnumerable <Book> cheapBooks = from book in bookCatalog.Books where book.Price.CompareTo(30m) < 0 select book; IEnumerable <Book> sortedBooks = from book in cheapBooks orderby book.Title select book; Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("SORTED CHEAP BOOKS (Chain of LINQ Queries)"); Console.WriteLine("******************************************"); foreach (Book book in sortedBooks) { string title = book.Title; decimal price = book.Price; Console.WriteLine(title + ": $" + price); } }
private static void DeletingData( ) { BookCatalog bookCatalog = new BookCatalog( ); Category category = bookCatalog.Categories.Single(c => c.Name == "Java"); bookCatalog.Categories.DeleteOnSubmit(category); bookCatalog.SubmitChanges( ); Console.WriteLine("*** DeletingData: Removed " + category.Name + "Category"); }
public static void UpdateExistingData( ) { BookCatalog bookCatalog = new BookCatalog( ); Category category = bookCatalog.Categories.Single(c => c.Name == "Programming Practices"); category.Name = "Technical Practices"; bookCatalog.SubmitChanges( ); Console.WriteLine("*** UpdateExistingData: Category name updated to " + category.Name); }
private static void DeletingDataInMtoMRelationship( ) { BookCatalog bookCatalog = new BookCatalog( ); Book rubyBook = bookCatalog.Books.Single(book => book.Title.Contains("Programming Ruby 1.9")); bookCatalog.Books.DeleteOnSubmit(rubyBook); bookCatalog.SubmitChanges( ); Console.WriteLine(" *** DeletingDataInMtoMRelationship: Deleted " + rubyBook.Title); }
public void Remove( ) { BookCatalog.RemoveRecord(this); Author priorAuthor = Author; priorAuthor.BookAuthors.Remove(this); Book priorBook = Book; priorBook.BookAuthors.Remove(this); }
public static void AccessingDataFromaM1Relationship( ) { BookCatalog bookCatalog = new BookCatalog( ); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("BOOK CATEGORIES (Accessing Data from a M:1 Relationship)"); Console.WriteLine("*******************************************************"); foreach (var book in bookCatalog.Books) { string categoryName = book.Category.Name; Console.WriteLine(categoryName + " (" + book.Title + ")"); } }
private static void AddNewData( ) { Category category = new Category( ) { Name = "Java" }; BookCatalog bookCatalog = new BookCatalog( ); bookCatalog.Categories.InsertOnSubmit(category); bookCatalog.SubmitChanges( ); Console.WriteLine("*** AddNewData: Added " + category.Name + " Category, got Id " + category.Id); }
private static void RemovingDataIn1toMRelationship( ) { BookCatalog bookCatalog = new BookCatalog( ); Book book = bookCatalog.Books.Single(b => b.Title.Contains("Programming Ruby 1.9")); Category rubyCategory = bookCatalog.Categories.Single(cat => cat.Name == "Ruby"); rubyCategory.Books.Remove(book); bookCatalog.SubmitChanges( ); Console.WriteLine(" *** ReovingDataIn1toMRelationship: Removed books from " + rubyCategory.Name + ":"); Console.WriteLine(" ---- " + book.Title + " is now in category: '" + book.Category + "'"); }
public static IBookCatalogItem GetExistingItem(BookCatalog bookCatalog, IBookCatalogItem dataItem) { if (dataItem is Author) { return(bookCatalog.Authors.Single(author => author == dataItem as Author)); } if (dataItem is Book) { return(bookCatalog.Books.Single(book => book == dataItem as Book)); } if (dataItem is Category) { return(bookCatalog.Categories.Single(category => category == dataItem as Category)); } throw new Exception("Unknown data type"); }
public static void AccessingDataFroma1MRelationship( ) { BookCatalog bookCatalog = new BookCatalog( ); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("BOOKS IN A CATEGORY (Accessing Data from a 1:M Relationship)"); Console.WriteLine("************************************************************"); foreach (var category in bookCatalog.Categories) { Console.WriteLine(category.Name + ":"); foreach (Book book in category.Books) { string bookTitle = book.Title; Console.WriteLine(" - " + bookTitle); } } }
private static void MovingDataIn1toMRelationship( ) { BookCatalog bookCatalog = new BookCatalog( ); var books = bookCatalog.Books.Where(b => b.Title.Contains("Pro LINQ: Language Integrated Query in C#")); Category linqCategory = bookCatalog.Categories.Single(cat => cat.Name == "LINQ"); foreach (var book in books) { linqCategory.Books.Add(book); } bookCatalog.SubmitChanges( ); Console.WriteLine(" *** MovingDataIn1toMRelationship: Moved books to " + linqCategory.Name + ":"); foreach (var book in books) { Console.WriteLine(" ---- " + book.Title + " is now in category: " + book.Category.Name); } }
private static void Mto1Update( ) { BookCatalog bookCatalog = new BookCatalog( ); Category csharpCategory = bookCatalog.Categories.Single(cat => cat.Name == "C#"); var books = bookCatalog.Books.Where(b => b.Title.Contains("Pro LINQ: Language Integrated Query in C#")); foreach (var book in books) { book.Category = csharpCategory; } bookCatalog.SubmitChanges( ); Console.WriteLine("*** Mto1Update: " + csharpCategory.Name + " Category now holds the books:"); foreach (var book in csharpCategory.Books) { Console.WriteLine(" ---- " + book.Title); } }
public static void DeleteItem(BookCatalog bookCatalog, IBookCatalogItem itemToDelete) { if (!itemToDelete.CanDelete( )) { return; } IBookCatalogItem dataItem = GetExistingItem(bookCatalog, itemToDelete); if (dataItem is Author) { bookCatalog.Authors.DeleteOnSubmit(dataItem as Author); } if (dataItem is Book) { bookCatalog.Books.DeleteOnSubmit(dataItem as Book); } if (dataItem is Category) { bookCatalog.Categories.DeleteOnSubmit(dataItem as Category); } }
private static void RemovingDataInMtoMRelationship( ) { BookCatalog bookCatalog = new BookCatalog( ); Author bobMartin = bookCatalog.Authors.Single(author => author.Name == "Bob Martin"); Book xpExplained = bookCatalog.Books.Single(book => book.Title.Contains("Extreme Programming Explained")); bobMartin.Books.Remove(xpExplained); bookCatalog.SubmitChanges( ); Console.WriteLine(" *** RemovingDataInMtoMRelationship: Removed " + xpExplained.Title + " from " + bobMartin.Name); Console.WriteLine(" " + xpExplained.Title + " now has these authors:"); foreach (var author in xpExplained.Authors) { Console.WriteLine(" ---- " + author.Name); } Console.WriteLine(" " + bobMartin.Name + " now has these books:"); foreach (var book in bobMartin.Books) { Console.WriteLine(" ---- " + book.Title); } }
private static void AddingDataIn1toMRelationship( ) { IEnumerable <Book> books = new List <Book>( ) { new Book( ) { Title = "Essential Windows Presentation Foundation", Price = 44.99m }, new Book( ) { Title = "WPF In Action", Price = 40.99m } }; BookCatalog bookCatalog = new BookCatalog( ); bookCatalog.Books.InsertAllOnSubmit(books); bookCatalog.SubmitChanges( ); Category category = new Category( ) { Name = "WPF" }; foreach (var wpfBook in books) { category.Books.Add(wpfBook); } bookCatalog.Categories.InsertOnSubmit(category); try { bookCatalog.SubmitChanges( ); } catch { } // if we've already added this category, don't give an error when we run this method again Console.WriteLine(" *** AddingDataIn1toMRelationship: Added books to " + category.Name + ":"); foreach (var book in books) { Console.WriteLine(" ---- " + book.Title + " is now in category: " + book.Category.Name); } }
public static IBookCatalogItem AddNewItem(BookCatalog bookCatalog, ItemType type) { IBookCatalogItem dataItem = null; if (type == ItemType.Author) { dataItem = new Author( ); bookCatalog.Authors.InsertOnSubmit(dataItem as Author); } if (type == ItemType.Book) { dataItem = new Book( ); bookCatalog.Books.InsertOnSubmit(dataItem as Book); } if (type == ItemType.Category) { dataItem = new Category( ); bookCatalog.Categories.InsertOnSubmit(dataItem as Category); } return(dataItem); }
public static void AccessingDataFromaMmRelationship( ) { BookCatalog bookCatalog = new BookCatalog( ); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("BOOKS (Accessing Data from a M:M Relationship)"); Console.WriteLine("**********************************************"); foreach (var book in bookCatalog.Books) { string title = book.Title; decimal price = book.Price; string category = book.Category.Name; ICollection <Author> authors = book.Authors; ICollection <Book> otherBooksInCategory = book.Category.Books; Console.WriteLine(title + " $" + price + " (" + category + "):"); Console.Write(" - Author(s): "); foreach (var author in authors) { Console.Write(author.Name + ", "); } Console.Write("\r\n - Other Book(s) in Category: "); foreach (var otherBook in otherBooksInCategory) { Console.Write(otherBook.Title + ", "); } Console.WriteLine("\r\n"); } Console.WriteLine("AUTHORS (Accessing Data from a M:M Relationship)"); Console.WriteLine("************************************************"); foreach (var author in bookCatalog.Authors) { string name = author.Name; ICollection <Book> books = author.Books; Console.WriteLine(name + ":"); foreach (var book in books) { Console.WriteLine(" - " + book.Title); } } Console.WriteLine("CATEGORIES (Accessing Data from a M:M Relationship)"); Console.WriteLine("**************************************************"); foreach (var category in bookCatalog.Categories) { string name = category.Name; Console.WriteLine(name + ":"); foreach (var book in category.Books) { string bookTitle = book.Title; ICollection <Author> bookAuthors = book.Authors; Console.WriteLine(" - " + bookTitle + ":"); foreach (var author in bookAuthors) { Console.WriteLine(" - " + author.Name); } } Console.WriteLine(""); } }