static void Main(string[] args) { var books = new BookRepository().GetBooks(); //LINQ Query Operators var listBooks2 = from b in books where b.Price < 10 orderby b.Title select b.Title; //LIQN Extension Methods var listBooks = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); var book1 = books.SingleOrDefault(b => b.Title == "ASP.NET"); var book2 = books.FirstOrDefault(b => b.Title == "C# Advanced Topics"); var book3 = books.LastOrDefault(b => b.Title == "C# Advanced Topics"); var book4 = books.Skip(2).Take(3); var book5 = books.Count(); var book6 = books.Max(b => b.Price); var book7 = books.Min(b => b.Price); var book8 = books.Sum(b => b.Price); var book9 = books.Average(b => b.Price); System.Console.WriteLine(book9); }
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); // } // 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); foreach (var book in cheapBooks) { // Console.WriteLine(book.Title + " " + book.Price); Console.WriteLine(book); } var bookSingle = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); var bookFirst = books.FirstOrDefault(b => b.Title == "C# Advanced Topics"); var count = books.Count(); var maxPrice = books.Max(b => b.Price); Console.WriteLine(bookSingle == null); Console.WriteLine(bookFirst.Title + " " + bookFirst.Price); Console.WriteLine(maxPrice); /* * books.Where(); * books.Single(); * books.SingleOrDefault(); * * books.First(); * books.FirstOrDefault(); * * books.Last(); * books.LastOrDefault(); * * books.Min(); * books.Max(); * books.Count(); * books.Sum(); * books.Average(b => b.Price); * * books.Skip(2).Take(3); */ }
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(); // Without LINQ we had to do this //var cheapBooks = new List<Book>(); //foreach (var book in books) // if (book.Price < 10) // cheapBooks.Add(book); // With LINQ we can do this // 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); // cheapBooks is now an emumerable of type string foreach (var book in cheapBooks) { Console.WriteLine(book); } //Console.WriteLine(book.Title + " " + book.Price); var singleBook = books.Single(b => b.Title == "ASP.NET MVC"); // Must have one and only one object that satisfies condition var singleBookd = books.SingleOrDefault(b => b.Title == "ASP.NET MVC"); // returns match or default in this case null Console.WriteLine(singleBook.Title); var firstBook = books.First(b => b.Title == "C# Advanced Topics"); Console.WriteLine(firstBook.Title + " " + firstBook.Price); // also has FirstOrDefault var skipBooks = books.Skip(2).Take(3); foreach (var skipBook in skipBooks) { Console.WriteLine(skipBook.Title); } var count = books.Count(); var maxPrice = books.Max(b => b.Price); Console.WriteLine(maxPrice); //returns float Price 12 in this case var totalPrice = books.Sum(b => b.Price); Console.WriteLine(totalPrice); }
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; foreach (var book in cheaperBooks) { Console.WriteLine(book); // title of the 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); // title of the book } //this will throw error if no mathing condition var singleBook = books.Single(b => b.Title == "ASP.NET MVC"); //there for singleBook = books.SingleOrDefault(b => b.Title == "ASP.NET MVC"); if (singleBook != null) { Console.WriteLine(singleBook.Price); } var firstBook = books.First(); //or with predicate firstBook = books.First(b => b.Price > 10); //similary this method will throw an exeption if not found firstBook = books.FirstOrDefault(); var paginatedBooks = books.Skip(2).Take(1); var booksCount = books.Count(); var maxPrice = books.Max(b => b.Price); var minPrice = books.Min(book => book.Price); var sumOfPrices = books.Sum(b => b.Price); }
// Language Integrated Query // Give the ability to query object directly in C# // Objects in memory(collections etc), Dbs, XML, Data Sets static void Main(string[] args) { var books = new BookRepository().GetBooks(); // Linq Extension Methods Syntax var cheapBooks = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); // Linq Query Operators Syntax var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b.Title; var singleBook = books.SingleOrDefault(b => b.Title == "ASP.NET MVC"); // the or default handles if the condition is not met var first = books.First(b => b.Title == "C# Advanced Topics"); // also has an "OrDefault" verison //.Last does the opposite var pagedBooks = books.Skip(2).Take(3); Console.WriteLine("count: " + books.Count()); var mostExpensive = books.Max(b => b.Price); var mostCheap = books.Min(b => b.Price); var totalCost = books.Sum(b => b.Price); Console.WriteLine($"1.{mostExpensive} then 2.{mostCheap} then 3.{totalCost}"); foreach (var book in pagedBooks) { Console.WriteLine(book.Title); } //foreach (var book in cheapBooks) //{ // //Console.WriteLine((book.Title + " " + book.Price)); // Console.WriteLine(book); //} //Console.WriteLine("Desired book: " + singleBook.Title); Console.WriteLine(first.Title + " " + first.Price); }
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(); //var cheapBooks = new List<Book>(); //foreach (var book in books) //{ // if (book.Price < 10) // cheapBooks.Add(book); //} // 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); foreach (var book in cheaperBooks) { Console.WriteLine(book); } Console.WriteLine(); foreach (var book in cheapBooks) { Console.WriteLine(book); } Console.WriteLine(); var singleBook = books.Single(b => b.Title == "ASP.NET MVC"); Console.WriteLine(singleBook.Title); Console.WriteLine(); var singleOrDefaultBook = books.SingleOrDefault(b => b.Title == "ASP.NET MVS++"); Console.WriteLine(singleOrDefaultBook == null); Console.WriteLine(); var firstBook = books.First(b => b.Title == "C# Advanced Topics"); Console.WriteLine(firstBook.Title + " " + firstBook.Price); Console.WriteLine(); var skipAndTakeBooks = books.Skip(2).Take(3); foreach (var book in skipAndTakeBooks) { Console.WriteLine(book.Title + " " + book.Price); } 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); }
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(); // Searches collection for items with specific conditions var cheapBooks = books.Where(book => book.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title + " " + book.Price); } // Sorts items in collection var orderedBooks = books.OrderBy(b => b.Title); foreach (var book in orderedBooks) { Console.WriteLine(book.Title + " " + book.Price); } // Selects all items in collection and projects them by type fields var selectedBooks = books.Select(b => b.Title); foreach (var book in selectedBooks) { Console.WriteLine(book); } // C# common convention layout for multiple inline LINQ statements var booksLinq = books .Where(book => book.Price < 10) // Searches collection for items with specific conditions .OrderBy(b => b.Title) // Sorts items in collection .Select(b => b.Title); // Selects all items in collection and projects them by type fields // OR LINQ Query Operators var booksLinqQuery = from b in books where b.Price < 10 orderby b.Title select b.Title; // Another useful LINQ Extension Methods var singleBook = books.SingleOrDefault(b => b.Title == "ASP.NET MVC"); // returns single item or default value Console.WriteLine(singleBook != null ? singleBook.Title : "There is no such book in collection"); var firstBook = books.FirstOrDefault(b => b.Title == "Entity Framework"); Console.WriteLine(firstBook.Title + " " + firstBook.Price); var lastBook = books.LastOrDefault(b => b.Title == "Entity Framework"); Console.WriteLine(lastBook.Title + " " + lastBook.Price); var skipBooks = books.Skip(2).Take(3); foreach (var skipBook in skipBooks) { Console.WriteLine(skipBook.Title); } Console.WriteLine("Books count: " + books.Count()); Console.WriteLine("Books max price: " + books.Max(b => b.Price)); Console.WriteLine("Books min price: " + books.Min(b => b.Price)); Console.WriteLine("Sum of all books prices: " + books.Sum(b => b.Price)); Console.WriteLine("Average of all books prices: " + books.Average(b => b.Price)); }
static void Linq() { var books = new Linq.BookRepository().GetBooks(); // LINQ Query Operators // var cheapBooks = from b in books // where b.Price < 10 // orderby b.Title // select b.Title; // LINQ Extension Methods (This is a more used syntax and it is also more powerful) // var cheapBooks = books // .Where(b => b.Price < 10) // .OrderBy(b => b.Title) // .Select(b => b.Title); // foreach (var cheapBook in cheapBooks) // { // Console.WriteLine($"{cheapBook.Title} - € {cheapBook.Price}"); // Console.WriteLine(cheapBook); // } // var book = books.Single(b => b.Title == "ASP.NET MVC"); // Console.WriteLine(book.Title); // This is safer than Single // var book = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); // if (book != null) // { // Console.WriteLine(book.Title); // } // var book = books.First(b => b.Title == "C# Advanced Topics"); // Console.WriteLine($"{book.Title} - € {book.Price}"); // This is safer than First // var book = books.FirstOrDefault(b => b.Title == "C#++ Advanced Topics"); // if (book != null) // { // Console.WriteLine(book.Title); // } // var book = books.Last(b => b.Title == "C# Advanced Topics"); // Console.WriteLine($"{book.Title} - € {book.Price}"); // This is safer than Last // var book = books.LastOrDefault(b => b.Title == "C#++ Advanced Topics"); // if (book != null) // { // Console.WriteLine(book.Title); // } // var pagedBooks = books.Skip(2).Take(3); // foreach (var pagedBook in pagedBooks) // { // Console.WriteLine($"{pagedBook.Title} - € {pagedBook.Price}"); // } var count = books.Count(); Console.WriteLine(count); var maxPrice = books.Max(b => b.Price); var minPrice = books.Min(b => b.Price); Console.WriteLine(maxPrice); Console.WriteLine(minPrice); var totalPrice = books.Sum(b => b.Price); Console.WriteLine(totalPrice); var averagePrice = books.Average(b => b.Price); Console.WriteLine(averagePrice); }
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(); }
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) { //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(); }
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); }
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(); // 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(); }
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); * } * }*/ //LINQ Query Operators Syntax for more clean var cheaperBooks = from b in books where b.Price < 10 orderby b.Title select b; var cheaperBookTitles = from b in books where b.Price < 10 orderby b.Title select b.Title; //LINQ Extension Methods Syntax for more in depth var cheapBooks = books .Where(b => b.Price < 10) .OrderBy(b => b.Title); var cheapBookTitles = books .Where(b => b.Price < 10) .OrderBy(b => b.Title) .Select(b => b.Title); //books.OrderBy(b => b.Title); foreach (var book in cheapBooks) { Console.WriteLine(book.Title + " " + book.Price); } Console.WriteLine(); foreach (var book in cheapBookTitles) { Console.WriteLine(book); } Console.WriteLine(); var bookOne = books.Single(b => b.Title == "ASP.NET MVC"); Console.WriteLine(bookOne.Title + " " + bookOne.Price); Console.WriteLine(); var bookTwo = books.SingleOrDefault(b => b.Title == "ASP.NET MVC++"); if (bookTwo == null) { Console.WriteLine("null"); } else { Console.WriteLine(bookTwo.Title + " " + bookTwo.Price); } Console.WriteLine(); var bookThree = bookTwo == null?("null"): (bookTwo.Title + " " + bookTwo.Price); Console.WriteLine(bookThree); Console.WriteLine(); var bookFour = books.FirstOrDefault(b => b.Title == "C# Advanced Topics"); var bookFive = bookFour == null ? ("null") : (bookFour.Title + " " + bookFour.Price); Console.WriteLine(bookFive); Console.WriteLine(); var bookSix = books.LastOrDefault(b => b.Title == "C# Advanced Topics"); var bookSeven = bookSix == null ? ("null") : (bookSix.Title + " " + bookSix.Price); Console.WriteLine(bookSeven); Console.WriteLine(); var pagedBooks = books.Skip(2).Take(3); foreach (var book in pagedBooks) { Console.WriteLine(book.Title + " " + book.Price); } Console.WriteLine(); var count = books.Count(); Console.WriteLine(count + " books"); Console.WriteLine(); var max = books.Max(b => b.Price); Console.WriteLine("$" + max); Console.WriteLine(); var min = books.Min(b => b.Price); Console.WriteLine("$" + min); Console.WriteLine(); var sum = books.Sum(b => b.Price); Console.WriteLine("$" + sum); Console.WriteLine(); var avg = books.Average(b => b.Price); Console.WriteLine("$" + avg); //Func<int, int, int> add = (a, b) => a + b; //Console.WriteLine(add(2, 3)); }