public static void Main(string[] args) { var books = new BookRepository().GetBooks(); //var book = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); //var book = books.First(b => b.Title == "C# Advanced Topics"); var book = books.FirstOrDefault(b => b.Title == "Bleh"); var bookTwo = books.Last(b => b.Title == "C# Advanced Topics"); var pagedBooks = books.Skip(2).Take(3); var countBooks = books.Count(); var maxPrice = books.Max(b => b.Price); var sumBooks = books.Sum(b => b.Price); //Null because "Bleh" doesn't exist try { Console.WriteLine(book.Price); } catch (NullReferenceException) { Console.WriteLine("Book is null"); } Console.WriteLine(bookTwo.Price); foreach (var pageBook in pagedBooks) { Console.WriteLine("\n" + pageBook.Title + "| " + pageBook.Price); } Console.WriteLine("\nTotal Books: " + countBooks); Console.WriteLine("\nMax Price: " + maxPrice); Console.WriteLine("\nSum Price: " + sumBooks); }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); //LINQ query Operators var cheapers = from b in books //always the first where b.Price < 15 orderby b.Title select b.Title; //always the last //LINQ extension methods var cheapBooks = books .Where(a => a.Price < 15) .OrderBy(a => a.Title) .Select(b => b.Title + b.Price); books.Single(b => b.Title.Equals("Title 3")); //Single() return only and only the one object; if there isn't object like this, app thorws exception //SingleOrDefault() will return null if there is not equilant object, no exception books.First(c => c.Title == "Title 1"); //First() returns the fist on the collection that satisfies this condition // FirstOrDefault() return default value instead of exception books.Last(d => d.Title == "Title 2"); //Last() or LastOrDefault() books.Skip(1).Take(2); //skip the first one and take following 2 var count = books.Count(); //number of items in collection var mostexpensiveBook = books.Max(e => e.Price); foreach (var b in cheapBooks) { Console.WriteLine(b); } Console.ReadKey(); }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); // LINQ Query Operators var cheapBooks2 = from b in books // Defines b as books where b.Price < 10 // Filters books that are over $10 orderby b.Title // Orders the selection by title select b.Title; // Converts the object elements to strings (book title) // LINQ Query Methods (is more powerful, since query uses keywords for methods, and not all of them have one) var cheapBooks = books .Where(b => b.Price < 10) .OrderBy(books => books.Title) .Select(books => books.Title); foreach (var cheapBook in cheapBooks) { System.Console.WriteLine(cheapBook); } // Some more examples of LINQ Extension Methods var book = books.SingleOrDefault(book => book.Title == "El Principito"); // Unlike Where(), this gets only one object // There's a Single Method, but that throws an exception if it doesn't find anything System.Console.WriteLine(book == null); // Returns a book with that title, if not found returns null (default object value) var firstBook = books.First(); // Gets the first book object var lastBook = books.Last(); // They can also receive delegates as arguments var pagedBooks = books.Skip(2).Take(3); // It's gonna skip 2 books and store the following three var maxPrice = books.Max(b => b.Price); // Returns the max value specified (the price, in this case) var minPrice = books.Min(b => b.Price); // Returns the min value specified var sumPrice = books.Sum(b => b.Price); // Returns the sum of all elements (prices) var averagePrice = books.Average(b => b.Price); // Gets the average given value }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); //without linq //var cheapBooks = new List<Book>(); //foreach (var book in books) //{ // if (book.Price < 10) // cheapBooks.Add(book); //} //with linq Extension methods: var cheapBooks = books.Where(b => b.Price < 10).OrderBy(b => b.Title); //can chain linq! //.Select is used for "projections" or "transformations". //the below code transforms a list of books into a list of strings. var cheapBooksTitleString = books.Select(b => b.Title); //more reader-friendly layout: var readerFriendly = 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}"); } //with LINQ query Operators: var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b.Title; //============ //other LINQ methods var single = books.Single(b => b.Title == "Book 3"); //if it can't find, it will crash. It wants one and only one object, if you're not sure better to use... Console.WriteLine(single.Title); var singleOrDefault = books.SingleOrDefault(b => b.Title == "hmm doesnt exist"); //returns null if it can't find, better to avoid exception crashing Console.WriteLine(singleOrDefault == null); var first = books.First(); //gives first book var firstWithPredicate = books.First(b => b.Title == "Book 4"); //gives first book with that title, i.e. the one with price as $14. Console.WriteLine(firstWithPredicate.Price); var firstOrDefault = books.FirstOrDefault(b => b.Title == "Book 08089"); //if nothing matches the lambda condition, it returns null without throwing exception var last = books.Last(); var lastOrDefault = books.LastOrDefault(); var skip = books.Skip(2).Take(3); //this will skip the first 2, and take the next 3 to the new list. Console.WriteLine($"Skip:"); foreach (var book in skip) { Console.WriteLine(book.Title); } var count = books.Count(); //will be 5 var max = books.Max(b => b.Price); //what does max mean? in this case, highest price. Console.WriteLine(max); //this is the price itself. var min = books.Min(b => b.Price); Console.WriteLine(min); var sum = books.Sum(b => b.Price); //sum based on price of books Console.WriteLine(sum); var averagePrice = books.Average(b => b.Price); Console.WriteLine(averagePrice); //float! }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); // this is how you do it without LINQ // var cheapBooks = new List<Book>(); // foreach (var book in books) // { // if (book.Price < 10) // { // cheapBooks.Add(book); // } // } // LINQ Query Operators // equivalent to Extension Methods below var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b.Title; // LINQ Extension Methods: can chain methods // Select used for projections 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); } var singleBook = books.Single(b => b.Title == "ASP.NET MVC"); var singleOrDefaultBook = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); var firstBook = books.First(b => b.Title == "C# Advanced Topics"); var firstOrDefaultBook = books.FirstOrDefault(b => b.Title == "C# Advanced Topics++"); var lastBook = books.Last(b => b.Title == "C# Advanced Topics"); Console.WriteLine(singleBook.Title); Console.WriteLine(singleOrDefaultBook == null ? "null" : singleOrDefaultBook.Title); Console.WriteLine(firstBook.Title + " " + firstBook.Price); Console.WriteLine(firstOrDefaultBook == null ? "null" : firstOrDefaultBook.Title); Console.WriteLine(lastBook.Title + " " + lastBook.Price); Console.WriteLine("Paged Books:"); var pagedBooks = books.Skip(2).Take(3); foreach (var pagedBook in pagedBooks) { Console.WriteLine(pagedBook.Title); } var count = books.Count(); Console.WriteLine(count); var maxPrice = books.Max(b => b.Price); Console.WriteLine(maxPrice); var minPrice = books.Min(b => b.Price); Console.WriteLine(minPrice); var sumPrice = books.Sum(b => b.Price); Console.WriteLine(sumPrice); }
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; books.Single(b => b.Title == "ASP.NET MVC"); // one object books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); //no books match this condition, function returns null Console.WriteLine(books == null); var book = books.First(b => b.Title == "C# Advanced Topics"); // First which match books.FirstOrDefault(b => b.Title == "C# Advanced Topics"); // null if no books match books.Last(b => b.Title == "C# Advanced Topics"); books.LastOrDefault(b => b.Title == "C# Advanced Topics"); // null if no books match var pagedBooks = books.Skip(2).Take(3); // skip 2 object and take 3 foreach (var pagedBook in pagedBooks) { Console.WriteLine(pagedBook.Title); } var countOfBooks = books.Count(); var maxPrice = books.Max(b => b.Price); var minPrice = books.Min(b => b.Price); var totalPrices = books.Sum(b => b.Price); // books.Where() <- to filter and retun a list of books to match to a given condition // books.Single() <- to filter and return a one book // books.SingleOrDefault() <- if don't find book return null // books.First() // books.FirstOrDefault() // books.Last() // books.LastOrDefault() // books.Min() // books.Max() // books.Count() // books.Sum() // books.Average(b => b.Price) // books.Skip(5).Take(3) // 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); // } }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); var cheapBooks = new List <Book>(); foreach (var book in books) { if (book.Price < 10) { cheapBooks.Add(book); } } // ŞİMDİ AYNI FİLTRELEMEYİ LINQ İLE YAPALIM // LINQ Extension Methods var cheapBooks1 = books.Where(b => b.Price < 10); // Lambda Expression ve where sorgusu // orderby kullanalım var cheapBooks2 = books.OrderBy(b => b.Title); // ucuca ekleyip, hem fiyata göre filtreleyip, hem Title a gore sıralayalım var cheapBooks3 = books.Where(b => b.Price < 10).OrderBy(b => b.Title); // ucuca ekleyip, hem fiyata göre filtreleyip, hem Title a gore sıralayalım. Select ekleyelim. // select projection veya transformation için kullanılıyormuş // sadece Title listesi oluştu yani string generic listesi var cheapBooks4 = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); // (Yukarıdaki şekildeki gibi nokta, altsatırda devam etmeye, LINQ Extension Methods diyoruz.) // Single ile sadece tek birşey seçebiliriz. Fakat dikkat et, eğer o filtren bulunamazsa, app hata verir önlem de almamışsan var cheapBooks6 = books.Single(b => b.Title == "ASP.NET MVC"); // O objectin olmaması riski de varsa, SingleOrDefault kullanırız. Alttaki örnekte default olarak null gelir bulunamazsa... var cheapBooks7 = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); // ++ eklediigimiz için bulunamıycak. // First, aynısından sadece ilkini bulur - bulamazsa hata verir var cheapBooks8 = books.First(b => b.Title == "C# Advanced Topics"); // FirstOrDefault, ilki, ama hiç bulamazsa default olarak bu örnekte null var cheapBooks9 = books.FirstOrDefault(b => b.Title == "C# Advanced Topics"); // Last, aynısından sadece sonuncuyu bulur - bulamazsa hata verir var cheapBooks10 = books.Last(b => b.Title == "C# Advanced Topics"); // LastOrDefault, sonuncusu, ama hiç bulamazsa default olarak bu örnekte null var cheapBooks11 = books.LastOrDefault(b => b.Title == "C# Advanced Topics"); // Skip() ve Take() - ilk 2 tanesini atla, sonra 3 tanesini al var cheapBooks12 = books.Skip(2).Take(3); // SQL deki gibi Aggregate Func da var. mesela Count() var bookCount = books.Count(); // SQL deki Max() - tabi price a göre max olmasını biz söylüycez - float type a return ediyor, çünkü class Books ta, Price type float var highestPrice = books.Max(b => b.Price); // type: float // Min() var minPrice = books.Min(b => b.Price); // type: float // Sum() - tüm kitapların fiyatı toplamı var totalPrice = books.Sum(b => b.Price); // type: float // Average() - tüm kitapların ortalama fiyatı var avgPrice = books.Average(b => b.Price); // type: float foreach (var book in cheapBooks) { Console.WriteLine(book.Title + " " + book.Price); } foreach (var book in cheapBooks4) { Console.WriteLine(book); } foreach (var book in cheapBooks12) { Console.WriteLine(book.Title + " " + book.Price); } // LINQ Query Methods var cheapBooks5 = from b in books where b.Price < 10 orderby b.Title select b.Title; }
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); }