static void Main(string[] args) { var books = new BookRepo().GetBooks(); //Examples.ExampleMethod(new BookRepo()); // LINQ Extension Methods //var query = books // .SingleOrDefault(b => b.Title == "Harry Potter and the Philospher's Stone").Title; //var query = books // .FirstOrDefault(b => b.Price > 14).Title; //var pagedBooks = books // .Skip(2) // .Take(3); //foreach (var pagedBook in pagedBooks) //{ // Console.WriteLine(pagedBook.Title + " " + pagedBook.Format); //} //var count = books.Count(); //Console.WriteLine(count); //var costToBuyAllBooks = books.Sum(b => b.Price); //var minPrice = books.Min(b => b.Price); //var maxPrice = books.Max(b => b.Price); //Console.WriteLine(maxPrice + " + " + minPrice); //Console.WriteLine("Cost to buy all books: £{0}", costToBuyAllBooks); var averageCost = books.Average(b => b.Price); Console.WriteLine(averageCost); }
public static void ExampleMethod (BookRepo bookRepo) { var books = bookRepo.GetBooks(); // Linq Query Operators var booksUnder13Quid = from b in books // From first where b.Price < 10 // Filter orderby b.Title // Order select b; // Select always at the end. // Linq extension methods. var booksUnder15Quid = books .Where(b => b.Price < 15) .OrderBy(b => b.Title) .Select(b => b.Title); // Multiple linq queries should be broken like so... // Select appears to take an element of the IEnumerable type and forms a new list using only that parameter? //foreach (var book in booksUnder15Quid) // //Console.WriteLine("{0} \t| {1} | {2}", book.Title, book.Price, book.Format); // Console.WriteLine(book); foreach (var book in booksUnder13Quid) Console.WriteLine(book.Title); }
static void Main(string[] args) { var books = new BookRepo().GetBooks(); //var cheapBooks = books.Where(b => b.Price < 10); // awesome! //var cheapBooks = books.Where(b => b.Price < 10).OrderBy(b => b.Title); //LINQ QUERY OPERATOR Console.WriteLine("Linq Query Operator"); var cheapBooksAgain = from b in books where (b.Price < 10) orderby(b.Title) select(b.Title); Console.WriteLine("LINQ QUERY OPERATOR"); foreach (var x in cheapBooksAgain) { Console.WriteLine(x); } Console.WriteLine(); //LINQ EXTENSION METHODS var cheapBooks = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); Console.WriteLine("LINQ EXTENSION METHODS"); Console.WriteLine("CHEAP"); foreach (var book in cheapBooks) { //Console.WriteLine("Less than 10 dollars " + book.Title + " " + book.Price); Console.WriteLine(book); } Console.WriteLine(); Console.WriteLine("Single/Default"); var singleOrDefault = books.SingleOrDefault(b => b.Title == "Abdul Bari"); Console.WriteLine(singleOrDefault != null); // True, i.e. yes the book is in there Console.WriteLine(); Console.WriteLine("First/Default"); //var first = books.First(); var first = books.First(b => b.Title == "Mosh Hamedani"); Console.WriteLine(first.Title + " " + first.Price); var firstOrDefault = books.FirstOrDefault(b => b.Title == "Mosh Hamedani"); Console.WriteLine(firstOrDefault.Title + " " + firstOrDefault.Price); Console.WriteLine(); Console.WriteLine("Skip/Take"); var skipTake = books.Skip(2).Take(3); foreach (var x in skipTake) { Console.WriteLine(x.Title + " " + x.Price); } Console.WriteLine(); Console.WriteLine("MIN/MAX"); var minPrice = books.Min(b => b.Price); Console.WriteLine(minPrice); var maxPrice = books.Max(b => b.Price); Console.WriteLine(maxPrice); Console.WriteLine(); Console.WriteLine("Sum"); var totalPrices = books.Sum(b => b.Price); Console.WriteLine(totalPrices); Console.WriteLine(); Console.WriteLine("Average Price"); var avgPrice = books.Average(b => b.Price); Console.WriteLine(avgPrice); // filter collections // without LINQ //var cheapBooks = new List<Book>(); //foreach (var book in books) //{ // if (book.Price < 10) // { // cheapBooks.Add(book); // } //} }