static void Main(string[] args) { var books = new BookRepository().GetBooks(); //LINQ Extensions Methods var cheapBooks = books .Where(b => b.Price < 10); var cheapBooksOrderedByTitle = books .Where(b => b.Price < 10) .OrderBy(b => b.Title); var stringListOfcheapBooksOrderedByTitle = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); //Fin LINQ Extensions Methods //LINQ Query Operators, this always start with "from" and always finish with the "select" var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b.Title; //Fin LINQ Query Operators //LINQ Extensions Methods, this method goes to an error if there is not any book with that title //to avoid it we can use "SingleOrDefault" line 40 var book = books.Single(b => b.Title == "ASP.NET MVC"); //If there is not object matching this condition the Default value will be return which in this case would be null var book1 = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); //These are used for paging data, this means skip to record/object and takes three var bookList = books.Skip(2).Take(3); foreach (var pagedBook in bookList) { Console.WriteLine(pagedBook.Title); } var priceOfMoreExpensiveBook = books.Max(b => b.Price); var priceOfCheapestBook = books.Min(b => b.Price); var totalPrices = books.Sum(b => b.Price); var averages = books.Average(b => b.Price); }
static void Main(string[] args) { //Another example //Worker work = new Worker(); #region LINQExample1 var books = new BookRepository().GetBooks(); //LINQ Query Operators var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b.Title; //Use link instead of the below code /*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.Title) .Select(b => b.Title); //foreach (var book in cheapBooks) // Console.WriteLine(book); // Console.WriteLine(book.Title + " " + book.Price); #endregion //will generate an error if item not found var book = books.Single(b => b.Title == "ASP.NET MVC"); //Will return Null var bookz = books.SingleOrDefault(b => b.Title == "ASP.NET MVC"); //Gets the first item with the indicated value //var bookzz = books.First(b => b.Title == "C# Advanced Topics"); var bookzz = books.FirstOrDefault(b => b.Title == "C# Advanced Topics"); var bookzzz = books.Skip(2).Take(3); Console.WriteLine(book.Title); }
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(); }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); var specificBook = books.Single(x => x.Title == "Book1"); var specificBook1 = books.SingleOrDefault(x => x.Title == "Book4"); if (specificBook1 == null) { Console.WriteLine("Not Found!"); } else { Console.WriteLine("Found!"); } Console.WriteLine(books.First(x => x.Price < 200).Title); Console.WriteLine(books.FirstOrDefault(x => x.Price < 100) == null); Console.WriteLine(); var filtered = books.Skip(2).Take(3); foreach (var item in filtered) { Console.WriteLine(item.Title); } var book = books.Max(b => b.Price); Console.WriteLine(book); var sum = books.Sum(b => b.Price); Console.WriteLine(sum); #region LINQ Exampes //LINQ Query //var cheap = from b in books // where b.Price < 200 // orderby b.Price // select b.Price; //Console.WriteLine(); //foreach (var item in cheap) //{ // Console.WriteLine(item); //} //Console.WriteLine(); //LINQ Extensions //var cheapBooks = books.Where(x => x.Price < 200); //var titles = books // .Where(x => x.Price < 200) // .Select(x => x.Title); //cheapBooks // .OrderBy(x => x.Price); //PrintList<Book>(books); //PrintList<Book>(cheapBooks); //Console.WriteLine(); //foreach (var item in titles) //{ // Console.WriteLine(item); //} #endregion }
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(); }