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) { var books = new BookRepository().Getbooks(); foreach (var book in books) { Console.WriteLine("Book Title :" + book.Title); Console.WriteLine("Price :" + book.Price + "$"); } //Display books less than 10$ without LINQ var cheapBooks = new List <Book>(); foreach (var book in books) { if (book.Price < 10) { cheapBooks.Add(book); } } Console.WriteLine(); Console.WriteLine("***BOOKS UNDER 10$***"); foreach (var cheapBook in cheapBooks) { Console.WriteLine("Book Title :" + cheapBook.Title); Console.WriteLine("Price :" + cheapBook.Price + "$"); } //With LINQ Extension Methods //Books under 10,Order By Price and show only Title var cheapBooksLINQ = books .Where(b => b.Price < 10) .OrderBy(b => b.Price) .Select(b => b.Title); Console.WriteLine(); Console.WriteLine("***BOOKS UNDER 10$ with LINQ***"); foreach (var cheapBook in cheapBooksLINQ) { Console.WriteLine("Book Title :" + cheapBook); } //With LINQ Query Operators var cheaperBooksLINQ = from b in books where b.Price < 10 orderby b.Price select b; Console.WriteLine(); Console.WriteLine("***BOOKS UNDER 10$ with LINQ***"); foreach (var cheaperBook in cheaperBooksLINQ) { Console.WriteLine("Book Title :" + cheaperBook.Title); Console.WriteLine("Price :" + cheaperBook.Price); } }
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; //LINQ extension methods var cheapBooks = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); //var book = books.Single(b => b.Title == "ASP.NET MVC++");//Burada patlayacaktır. Single tek bir sonuç arar. Birden çok sonuç dönerse de patlar. //var book = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++");//returns null //var book = books.First(b => b.Title == "C# Advanced Topics"); //var bookLast = books.Last(b => b.Title == "C# Advanced Topics"); //Console.WriteLine(book.Title+" "+book.Price+"\n"+ bookLast.Title + " " + bookLast.Price); var pagedBooks = books.Skip(2).Take(3); foreach (var b in pagedBooks) { Console.WriteLine(b.Title); } var count = books.Count(); var maxBook = books.Max(b => b.Price); var minBook = books.Min(b => b.Price); var total = books.Sum(b => b.Price); //Console.WriteLine(maxBook); //Console.WriteLine(minBook); //Console.WriteLine(total); }
static void Main(string[] args) { var library = new BookRepository().GetBooks(); // With LINQ Query Operators var cheaperBooks = from book in library where book.Price < 10 orderby book.Title select book.Title; // With LINQ extension methods var cheapBooks = library .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); Nullable <DateTime> date = null; DateTime? dateAgain = null; foreach (var title in cheapBooks) { Console.WriteLine(title); } }
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)}"); }
//Definition of LINQ //Stands for Language Integrated Query //Capability to query objects //Usage //You can query objects in memory (LINQ to objects), Databases (LINQ to Entities), XML (LINQ to XML), ADO.NET Data Sets (LINQ to Data Sets) 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; //LINQ Extension Methods books.Where(x => x.Price < 10).ToList().ForEach(y => Console.WriteLine(y.Title)); }
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(); //Linq query operators var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b; //Linq extension methods var cheapestBooks = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); var single = books.First(b => b.Title == "C# Advanced topics"); Console.WriteLine(single.Title + " " + single.Price + "\n"); var count = books.Count(); Console.WriteLine(count); var max = books.Max(b => b.Price); Console.WriteLine(max); var min = books.Max(books => books.Price); Console.WriteLine(min); var totalPrices = books.Sum(b => b.Price); Console.WriteLine(totalPrices); foreach (var book in cheapestBooks) { // Console.WriteLine(book.Title + " " + book.Price); Console.WriteLine(book); } }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); 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); } /*var book = books.Single(b.Title == "ASP.NET MVC"); * Console.WriteLine(book.Title); * * var book = books.SingleOrDefault(b.Title == "ASP.NET MVC"); * Console.WriteLine(book.Title); * * var book = books.First(b.Title == "ASP.NET MVC"); * Console.WriteLine(book.Title + " " + book.Price); * * var pagedBooks = books.Skip(2).Take(3); * foreach (var pagedBook in pagedBooks) * Console.WriteLine(pagedBook.Title); * * var maxPrice = books.Max(b => b.Price); * var minPrice = books.Min(b => b.Price); * var count = books.Count(); * Console.WriteLine(maxPrice); * Console.WriteLine(minPrice); * Console.WriteLine(count); */ /* LINQ extension methods * var cheapBooks = books * .Where(b => b.price > 10) * .OrderBy(b => b.Title) * .Select(b => b.Title); */ /* LINQ query operators * var cheapBooks = * from b in books * where b.Price < 10 * orderby b.Title * select b.title; */ }
//Lesson 6 : LINQ static void Main(string[] args) { //LINQ Extension Methods var books = new BookRepository().GetBooks(); var cheapBooks = books.Where(x => x.Price < 10).Select(x => x.Title); foreach (var book in cheapBooks) { //Console.WriteLine(book.Title); Console.WriteLine(book); } //LINQ Queries var cheapBooks2 = from b in books where b.Price < 10 select b.Title; foreach (var book in cheapBooks2) { //Console.WriteLine(book.Title); Console.WriteLine(book); } Console.WriteLine("-----------"); //Return a single book //var singleBook = books.Single(b => b.Title == "Title 3"); var singleBook = books.SingleOrDefault(b => b.Title == "Title 3"); Console.WriteLine(singleBook.Title); //Skip data Console.WriteLine("-----------"); var chosenBooks = books.Skip(2); foreach (var book in chosenBooks) { 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(); }
public void Exercise1() { var books = new BookRepository().GetBooks(); //LINQ Query operators var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b.Title; // LINQ extension methods var cheapBooks = books.Where(x => x.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); foreach (var book in cheapBooks) { Console.WriteLine(book); } Console.ReadLine(); }
static void Main(string[] args) { var people = new List <Person>() { new Person() { Name = "Anastasia", Job = "Developer" }, new Person() { Name = "Dzmitry", Job = "Developer" }, new Person() { Name = "Peter", Job = "Bookeeper" }, new Person() { Name = "Josh", Job = "Cook" }, new Person() { Name = "Aaron", Job = "Cook" }, new Person() { Name = "Anastasia", Job = "Server" }, }; var anastasias = from p in people where p.Job == "Developer" select p; foreach (var anastasia in anastasias) { Console.WriteLine(anastasia.Job); Console.WriteLine(anastasia.Name); } var books = new BookRepository().GetBooks(); //With select you can select by property var cheapBooksTitles = books .Where(b => b.Price < 10) .Where(b => b.Price < 10) .Select(b => b.Title); // Linq query Operator like a SQL var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b.Title; // Returning Single Book or Operator || SingleOrDefault will return null as a value var singleBook = books.SingleOrDefault(b => b.Title == "ASP.NET MVC"); Console.WriteLine(singleBook.Price); // can find the first value that matche the query || FirstOrDefalult Console.WriteLine(books.First(b => b.Title == "C# Advanced Topics").Title); // Last || LastOrDefault Console.WriteLine(books.Last(b => b.Title == "C# Advanced Topics").Title); // Skiping the data Console.WriteLine(books.Skip(2).Take(3).Select(b => b.Title)); // Aggregators Console.WriteLine("Aggregators"); Console.WriteLine(books.Count()); //Max Price Console.WriteLine(books.Max(x => x.Price)); // Cheppest Price Console.WriteLine(books.Min(x => x.Price)); // Sum Console.WriteLine(books.Sum(b => b.Price)); // Aggregate fuinctions var list = new List <int>() { 13, 18, 25, 12, 16 }; list.Aggregate((a, b) => a + b); // Get Avarage var avgPrice = books.Average(b => b.Price); Console.WriteLine("The avarage book price is {0} ", avgPrice); foreach (var s in books.Skip(2).Take(3).Select(b => b.Title)) { Console.WriteLine("Skipped Book {0} ", s); } foreach (var cheapBooksTitle in cheapBooksTitles) { Console.WriteLine(cheapBooksTitle); } foreach (var book in books.OrderBy(b => b.Title).Where(b => b.Price < 10)) { Console.WriteLine(book.Title); Console.WriteLine(book.Price); } }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); //LINQ Query Operators //slightly worse, keywords translate to extension methods //anyway, and there isnt a keyword for everything. although it is //friendlier and cleaner. var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b; //LINQ Extension Methods var cheapBooks = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); var bookMVC = books.FirstOrDefault(b => b.Title == "C# Advanced Topics"); //var bookMVC = books.LastOrDefault(b => b.Title == "C# Advanced Topics"); //var bookMVC = books.SingleOrDefault(b => b.Title == "ASP.NET MVC+"); if (bookMVC != null) { Console.WriteLine(bookMVC.Title + " " + bookMVC.Price); } Console.WriteLine("---------------------"); Console.WriteLine("Now trying skip and take"); Console.WriteLine("---------------------"); var pagedBooks = books.Skip(2).Take(3); foreach (var pagedBook in pagedBooks) { Console.WriteLine(pagedBook.Title); } var count = books.Count(); Console.WriteLine("---------------------"); Console.WriteLine(count); var max = books.Max(b => b.Price); Console.WriteLine("---------------------"); Console.WriteLine(max); var min = books.Min(b => b.Price); Console.WriteLine("---------------------"); Console.WriteLine(min); var totalCost = books.Sum(b => b.Price); Console.WriteLine("---------------------"); Console.WriteLine(totalCost); var average = books.Average(b => b.Price); Console.WriteLine("---------------------"); Console.WriteLine(average); //Workaround, non-LINQ version // //var cheapBooks = new List<Book>(); //foreach (var book in books) //{ // if (book.Price < 10) // cheapBooks.Add(book); //} //foreach (var book in cheapBooks) //{ // Console.WriteLine("---------------------"); // Console.WriteLine(book.Title + " " + book.Price); // Console.WriteLine(book); }
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(); }
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); }