示例#1
0
        public static void Main()
        {
            try
            {
                using (var catalog = new Catalog())
                {
                    catalog.Clean();

                    // Create
                    CatalogInitializer.Seed().ForEach(book => catalog.Books.Add(book));
                    catalog.SaveChanges();

                    // Read
                    var booksInCatalog = catalog.Books
                        .OrderBy(book => book.Published);

                    Console.WriteLine(booksInCatalog.FormatValues());
                    Console.Write(Environment.NewLine);
                    Console.WriteLine("Press any key to continue . . .");
                    Console.ReadKey(true);
                    Console.Clear();

                    // Update
                    var bookToUpdate = catalog.Books
                        .Where(book => book.Title.Contains("Depth"))
                        .SingleOrDefault();

                    bookToUpdate.Title += ", Second Edition";
                    catalog.SaveChanges();

                    // Delete
                    var booksToDelete = catalog.Books
                        .Where(book => book.InStock == false)
                        .ToList();

                    booksToDelete.ForEach(book => catalog.Books.Remove(book));
                    catalog.SaveChanges();

                    var booksInStock = catalog.Books
                        .OrderBy(book => book.Published);

                    Console.WriteLine(booksInStock.FormatValues());
                }
            }
            catch (Exception exception)
            {
                Console.Write(Environment.NewLine);
                Console.WriteLine(string.Format("Exception: {0}", exception.ToString()));
            }
            finally
            {
                Console.Write(Environment.NewLine);
                Console.WriteLine("Press any key to continue . . .");
                Console.ReadKey(true);
            }
        }
示例#2
0
        static void Main()
        {
            try
            {
                using (var catalog = new Catalog())
                {
                    var books = from book in catalog.Books
                                where book.InStock == true
                                orderby book.Published
                                select book;

                    var txt = new StringBuilder();
                        txt.AppendLine(String.Format("{0,-37} {1,-23} {2,10} {3,5}", "-".Repeat(37), "-".Repeat(23), "-".Repeat(10), "-".Repeat(5)));
                        txt.AppendLine(String.Format("{0,-37} {1,-23} {2,-10} {3,-5}", "Title", "Author", "Published", "Pages"));
                        txt.AppendLine(String.Format("{0,-37} {1,-23} {2,10} {3,5}", "-".Repeat(37), "-".Repeat(23), "-".Repeat(10), "-".Repeat(5)));

                    foreach (var book in books)
                    {
                        txt.AppendLine(String.Format("{0,-37} {1,-23} {2,10} {3,5}", book.Title, book.Author, book.Published.ToShortDateString(), book.Pages));
                    }
                        txt.AppendLine(String.Format("{0,-37} {1,-23} {2,10} {3,5}", "-".Repeat(37), "-".Repeat(23), "-".Repeat(10), "-".Repeat(5)));

                    Console.WriteLine(txt.ToString());
                }
            }
            catch (Exception err)
            {
                Console.Write(Environment.NewLine);
                Console.WriteLine(String.Format("Exception: {0}", err.Message));
            }
            finally
            {
                Console.Write(Environment.NewLine);
                Console.Write("Press any key to continue . . .");
                Console.ReadKey(true);
            }
        }