Exemple #1
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            // LINQ Extension Methods
            var book = books.LastOrDefault(b => b.Title == "C# Advanced Topics");

            var pagedBooks = books.Skip(2).Take(1);
            var count      = books.Count();

            var maxPrice    = books.Max(b => b.Price);
            var minPrice    = books.Min(b => b.Price);
            var totalPrices = books.Sum(b => b.Price);

            Console.WriteLine(book.Title + " " + book.Price);
            Console.WriteLine(book == null);

            foreach (var pagedBook in pagedBooks)
            {
                Console.WriteLine(pagedBook.Title + " " + pagedBook.Price);
            }
            Console.WriteLine(count);
            Console.WriteLine(maxPrice);
            Console.WriteLine(minPrice);
            Console.WriteLine(totalPrices);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            // LINQ Query Operators [always start with the from and end with select]
            var cheaperBooks = from b in books
                               where b.Price < 10
                               orderby b.Title
                               select b.Title;

            // With LINQ Extension Methods
            var cheapBooks = books
                             .Where(b => b.Price < 10)
                             .OrderBy(b => b.Title)
                             .Select(b => b.Title);

            foreach (var book in cheapBooks)
            {
                //Console.WriteLine(book.Title + " " + book.Price);
                Console.WriteLine(book);
            }

            // SINGLE OBJECT
            var book1 = books.SingleOrDefault(books => books.Title == "ASP.NET MVC");

            Console.WriteLine(book1.Title);

            // first object
            var book2 = books.First(b => b.Title == "How to code");

            Console.WriteLine(book2.Title + " " + book2.Price);


            // last
            var book3 = books.LastOrDefault(books => books.Title == "ASP.NET MVC");

            Console.WriteLine(book3.Title + " " + book3.Price);

            // skip
            var pagedBooks = books.Skip(2).Take(3);

            Console.WriteLine("\nLast 3 books: ");
            foreach (var pageBook in pagedBooks)
            {
                Console.WriteLine(pageBook.Title);
            }

            // count
            Console.WriteLine($"There are {books.Count()} books. ");

            Console.WriteLine($"The highest priced book is P{books.Max(b => b.Price)}");
        }
        static void Main(string[] args)
        {
            var books      = new BookRepository().GetBooks();
            var cheapBooks = books.Where(b => b.Price < 10);

            // LINQ Query Operator
            var cheaperBooks =
                from b in books
                where b.Price < 10
                orderby b.Title
                select b.Title;

            // LINQ Methods can be chained
            var orderedByTitle = books
                                 .OrderBy(b => b.Title)
                                 .Select(b => b.Title);

            foreach (var b in orderedByTitle)
            {
                Console.WriteLine(b);
            }

            // Other Examples
            var result1 = books.Single(book => book.Title == "A");
            var result2 = books.SingleOrDefault(b => b.Title == "A");

            var result3 = books.FirstOrDefault(b => b.Title == "A");
            var result4 = books.LastOrDefault(b => b.Title == "A");

            var result5 = books.Skip(2).Take(3);

            var result6 = books.Count();
            var result7 = books.Max(b => b.Price);
            var result8 = books.Sum(b => b.Price);
            var result9 = books.Average(b => b.Price);



            Console.ReadLine();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            //LINQ Query Operators
            var cheaperBooks = from b in books
                               where b.Price < 10
                               orderby b.Title
                               select b.Title;

            /*---the same -----*/

            //LINQ Extension Methods
            var cheapBooks = books
                             .Where(b => b.Price < 10)
                             .OrderBy(b => b.Title)
                             .Select(b => b.Title);

            foreach (var bk in cheapBooks)
            {
                // Console.WriteLine(book.Title);
                Console.WriteLine(bk);
            }


            /*------------------------------------------------------------------*/
            var book  = books.Single(b => b.Title == "ASP.NET MVC");            //throw an exception if nothing found
            var book2 = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); //return null if nothing found

            Console.WriteLine(book.Title);
            Console.WriteLine(book2 == null ? "null" : book2.Title);

            /*-------------------------------------------------------------------*/

            var book3 = books.First();                                     //first element of the collection
            var book4 = books.First(b => b.Title == "C# Advanced Topics"); //first element of the collection
            var book5 = books.FirstOrDefault();                            //return null if nothing found

            Console.WriteLine(book3.Title);
            Console.WriteLine(book4.Price);
            Console.WriteLine(book5 == null ? "null" : book5.Title);

            var book6 = books.Last();                                     //first element of the collection
            var book7 = books.Last(b => b.Title == "C# Advanced Topics"); //first element of the collection
            var book8 = books.LastOrDefault();                            //return null if nothing found

            Console.WriteLine(book6.Title);
            Console.WriteLine(book7.Price);
            Console.WriteLine(book8 == null ? "null" : book8.Title);

            /*-------------------------------------------------------------------*/

            var books9 = books.Skip(2).Take(3); //used for paging data

            //skip 2 records or two objects and take 3
            foreach (var bb in books9)
            {
                Console.WriteLine(bb.Title);
            }


            /* --------------------------------------------------------------------*/

            var count = books.Count();

            Console.WriteLine(count);

            var maxPrice          = books.Max(b => b.Price);
            var minPrice          = books.Min(b => b.Price);
            var sumPriceofAllBook = books.Sum(b => b.Price);
            var average           = books.Average(b => b.Price);

            Console.WriteLine("maxPrice : {0} and min price : {1} and sum of all books : {2}, and finally average = {3}", maxPrice, minPrice, sumPriceofAllBook, average);

            Console.ReadLine();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            /*
             * This can be done in one line using LINQ:
             * var cheapBooks = new List<Book>();
             *
             * foreach(var book in books)
             * {
             *  if(book.Price < 10)
             *  {
             *      cheapBooks.Add(book);
             *  }
             * }
             */

            // LINQ Extension methods
            var cheapBooks = books
                             .Where(b => b.Price < 10)
                             .OrderBy(b => b.Price)
                             .Select(b => b.Title);

            // LINQ Query operators
            var cheaperBooks = from b in books
                               where b.Price < 10
                               orderby b.Title
                               select b.Title;

            foreach (var book in cheapBooks)
            {
                //Console.WriteLine(book.Title + " " + book.Price);
                Console.WriteLine(book);
            }

            Console.WriteLine();

            foreach (var book in cheaperBooks)
            {
                //Console.WriteLine(book.Title + " " + book.Price);
                Console.WriteLine(book);
            }

            Console.WriteLine();

            var book1 = books.SingleOrDefault(b => b.Title == "ASP.NET MVC+");

            Console.WriteLine(book1);

            var book2 = books.FirstOrDefault(b => b.Title == "C# Advanced Topics");

            Console.WriteLine(book2.Title + " " + book2.Price);

            var book3 = books.LastOrDefault(b => b.Title == "C# Advanced Topics");

            Console.WriteLine(book3.Title + " " + book3.Price);

            Console.WriteLine();

            var books4 = books.Skip(2).Take(3);

            foreach (var book in books4)
            {
                Console.WriteLine(book.Title);
            }

            Console.WriteLine();

            var count = books.Count();

            Console.WriteLine(count);

            Console.WriteLine();

            var maxPrice = books.Max(b => b.Price);

            Console.WriteLine(maxPrice);

            Console.WriteLine();

            var minPrice = books.Min(b => b.Price);

            Console.WriteLine(minPrice);

            Console.WriteLine();

            var totalPrice = books.Sum(b => b.Price);

            Console.WriteLine(totalPrice);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            // LINQ query operators
            // always start with from and end with select
            var cheapBooks =
                from b in books
                where b.Price < 10
                orderby b.Title
                select b.Title;

            foreach (var book in cheapBooks)
            {
                Console.WriteLine(book);
            }

            var singleBook = books.SingleOrDefault(b => b.Title == "Book 2"); // if there is no book2 it will return the default which is null

            Console.WriteLine("Single book: " + singleBook.Title);

            var firstBook = books.FirstOrDefault(b => b.Title == "Book 3");

            Console.WriteLine("First book that has title Book 3: " + firstBook.Price); // default is null

            var lastBook = books.LastOrDefault(b => b.Title == "Book 3");

            Console.WriteLine("Last book that has title Book 3: " + lastBook.Price);

            var pages = books.Skip(2).Take(3); // skips the first 2 books and takes the next 3

            foreach (var pagedBook in pages)
            {
                Console.WriteLine(pagedBook.Title);
            }

            var count = books.Count();

            Console.WriteLine("Count: " + count);

            var maxPrice = books.Max(b => b.Price); // returns the book with the max price

            Console.WriteLine("Max: " + maxPrice);

            var sumPrice = books.Sum(b => b.Price);

            Console.WriteLine("Total: " + sumPrice);


            // LINQ Extension methods

            /*            var cheapBooks = books.Where(b => b.Price < 10).OrderBy(b => b.Title).Select(b => b.Title);
             *
             *          foreach(var book in cheapBooks)
             *              Console.WriteLine(book);*/

            // after using select for the title, book is now a string because it returns an
            // IEnumerable of just the title of the books. This can no longer be used

            /*            foreach(var book in cheapBooks)
             *              Console.WriteLine(book.Title + " " + book.Price);*/

            // without LINQ

            /*            var cheapBooks = new List<Book>();
             *          foreach(var book in books)
             *          {
             *              if (book.Price < 10)
             *              {
             *                  cheapBooks.Add(book);
             *                  Console.WriteLine(book.Title + " " + book.Price);
             *              }
             *          }*/
        }