Exemplo n.º 1
0
        static void Main()
        {
            var books = new BookRepository().GetBooks();

            //LINQ Query Operators
            var booksArray = books as Book[] ?? books.ToArray();
            var cheaperBooks =
                from b in booksArray
                where b.Price < 10
                orderby b.Title
                select b.Title;

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

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

            var book = booksArray.Single(b => b.Title == "ASP.Net MVC");

            var lastBook = booksArray.LastOrDefault(b => b.Price > 9);

            var pagedBooks = booksArray.Skip(2).Take(3);

            foreach (var pagedBook in pagedBooks)
            {
                Console.WriteLine(pagedBook.Title);
            }
            Console.WriteLine(booksArray.Sum(b=>b.Price));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //Language Integrated Query

            var books = new BookRepository().GetBooks();

            //var cheaperBooks = new List<Book>();
            //foreach (var book in books)
            //{
            //    if (book.Price < 10)
            //        cheaperBooks.Add(book);
            //}

            //Linq Query Operators - bu kısım aslında arka planda Linq Extension Methods translate edilir
            var cheaperBooks =
                from b in books
                where b.Price < 10
                orderby b.Title
                select b.Title;//select b;

            //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);
            //Console.WriteLine(book.Title + " " + book.Price);

            Console.ReadLine();

            //var bookItem = books.Single(b => b.Title == "ASP.NET MVC++"); //null gelirse exception fırlatır
            //var bookItem = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++");//null gelme ihtimali varsa kullanılır
            //var bookItem = books.First(b => b.Title == "C# Advanced Topics");
            //var bookItem = books.FirstOrDefault(b => b.Title == "C# Advanced Topics");
            var bookItem = books.LastOrDefault(b => b.Title == "C# Advanced Topics");

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

            Console.ReadLine();

            //Good for paging
            var pagedBooks = books.Skip(2).Take(3);
            foreach (var book in pagedBooks)
            {
                Console.WriteLine(book.Title + " " + book.Price);
            }

            Console.ReadLine();

            var count = books.Count();
            Console.WriteLine(count);

            Console.ReadLine();

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

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

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

            var averagePrice = books.Average(b => b.Price);
            Console.WriteLine(averagePrice);

            Console.ReadLine();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            Console.WriteLine("-------------- all books ");
            foreach (var book in books)
            {
                Console.WriteLine($"{book.Title}, {book.Price}");
            }


            // LINQ Extension Methods

            var cheapBooks = books
                             .Where(b => b.Price < 20)
                             .OrderBy(b => b.Title);

            var cheapBooksStrings = books
                                    .Where(b => b.Price < 20)
                                    .OrderBy(b => b.Title)
                                    .Select(b => b.Title);

            Console.WriteLine("-------------- get books (objects) with LINQ Extension Methods");
            foreach (var book in cheapBooks)
            {
                Console.WriteLine($"{book.Title}, {book.Price}");
            }
            Console.WriteLine("-------------- get titles with LINQ Extension Methods");
            foreach (var book in cheapBooksStrings)
            {
                Console.WriteLine(book);
            }



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

            Console.WriteLine("-------------- get books (objects) with LINQ Query Operators");
            foreach (var book in cheapBooksWithQuery)
            {
                Console.WriteLine($"{book.Title}, {book.Price}");
            }

            var theBook = new Book {
                Title = "I dont exist", Price = 0
            };

            Console.WriteLine("-------------- Single");
            theBook = books.SingleOrDefault(b => b.Title == "Zoo");
            Console.WriteLine($"{theBook.Title}, {theBook.Price}");

            Console.WriteLine("-------------- SingleOrDefault");
            theBook = books.SingleOrDefault(b => b.Title == "zoo");
            Console.WriteLine(theBook == null);


            Console.WriteLine("-------------- First");
            theBook = books.FirstOrDefault(b => b.Price == 6.99m);
            Console.WriteLine($"{theBook.Title}, {theBook.Price}");

            var orderedBooks = books.OrderBy(b => b.Title);

            Console.WriteLine("-------------- First of ordered");
            theBook = orderedBooks.FirstOrDefault(b => b.Price == 6.99m);
            Console.WriteLine($"{theBook.Title}, {theBook.Price}");


            Console.WriteLine("-------------- FirstOrDefault");
            theBook = books.FirstOrDefault(b => b.Price == 123.99m);
            Console.WriteLine(theBook == null);


            Console.WriteLine("-------------- Skip, Take");
            var pagedBooks = books.Skip(3).Take(4);

            foreach (var book in pagedBooks)
            {
                Console.WriteLine($"{book.Title}, {book.Price}");
            }

            Console.WriteLine("-------------- Count");
            Console.WriteLine($"there are {books.Count} books");

            Console.WriteLine("-------------- Sum");
            Console.WriteLine($"you need {books.Sum(b => b.Price)} dollars to buy all the books");
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            Console.WriteLine("number of books:");
            var count = books.Count();

            Console.WriteLine(count);

            Console.WriteLine("name of books:");
            foreach (var b in books)
            {
                Console.WriteLine("Title: " + b);
            }

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

            Console.WriteLine("Cheapbooks:");
            foreach (var b in cheapBooks)
            {
                Console.WriteLine("Title: " + b);
            }

            Console.WriteLine("favourite book:");
            var book = books.Single(b => b.Title == "Solid Mechanics");

            Console.WriteLine(book.Title);

            Console.WriteLine("first book in collection:");
            var firstBook = books.First();

            Console.WriteLine("Title: " + firstBook.Title + ". Price: " + firstBook.Price);

            Console.WriteLine("last book in collection:");
            var lastBook = books.Last();

            Console.WriteLine("Title: " + lastBook.Title + ". Price: " + lastBook.Price);

            Console.WriteLine("books after skipping first three:");
            var remainingBooks = books.Skip(2).Take(2);

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

            Console.WriteLine("max price: ");
            var maxPrice = books.Max(b => b.Price);

            Console.WriteLine(maxPrice);

            Console.WriteLine("min price: ");
            var minPrice = books.Min(b => b.Price);

            Console.WriteLine(minPrice);

            Console.WriteLine("total price: ");
            var totalPrice = books.Sum(b => b.Price);

            Console.WriteLine(totalPrice);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            // 1. Display the list of books that are cheaper than 10$

            // Without LINQ
            //var cheapBooks = new List<Book>();

            //foreach(var book in books)
            //{
            //    if(book.Price < 10)
            //    {
            //        cheapBooks.Add(book);
            //    }
            //}

            // With LINQ
            var cheapBooks = books
                             .Where(b => b.Price < 10) // We use Where to filter collections
                             .OrderBy(b => b.Title)    // We use OrderBy and OrderByDescending to sort collections
                             .Select(b => b);          // We use Select for custom projections or transformations

            foreach (var cheapBook in cheapBooks)
            {
                Console.WriteLine($"{cheapBook.Title}: {cheapBook.Price}");
            }

            // 2. Display only one book with specific title
            var aspnetBook = books.Single(b => b.Title == "ASP.NET MVC"); // We use Single / SingleOrDefault to take only one element from the collection

            Console.WriteLine($"Title of book is {aspnetBook.Title} with price of {aspnetBook.Price}$");

            // 3. Give me the first book which title is "C# Advanced Topics"
            var firstInstanceOfCSharpAdvancedTopics = books.First(b => b.Title == "C# Advanced Topics"); // We use First / FirstOrDefault to take the first element of a collection returned that satisfied a specific predicate

            Console.WriteLine($"Title of book is {firstInstanceOfCSharpAdvancedTopics.Title} with price of {firstInstanceOfCSharpAdvancedTopics.Price}$");

            // 4. Skip the first two elements and take the next 3
            var pagedBooks = books
                             .Skip(2)
                             .Take(3);    // We use Skip and Take mainly for paging data

            foreach (var pagedBook in pagedBooks)
            {
                Console.WriteLine($"{pagedBook.Title}: {pagedBook.Price}");
            }

            // 5. Count the number of elements in a collection
            var noOfBooks = books.Count();

            Console.WriteLine($"Total number of books in collection: {noOfBooks}");

            // 6. Return the book with the maximum price
            var maxPriceBook = books.Max(b => b.Price);

            Console.WriteLine($"Maximum price of book is {maxPriceBook}$");

            // 7. Return total sum of prices of books in the collection
            var totalSumOfBookPrices = books.Sum(b => b.Price);

            Console.WriteLine($"Total sum of prices of books is: {totalSumOfBookPrices}$");

            // 8. Group books by title
            var groups = books
                         .GroupBy(b => b.Title)
                         .Select(g => new
            {
                Title = g.Key,
                Books = g
            });

            foreach (var group in groups)
            {
                Console.WriteLine($"Title: {group.Title}");

                foreach (var book in group.Books)
                {
                    Console.WriteLine($"\t Book Price: {book.Price}");
                }
            }

            Console.ReadLine();
        }