static void Main(string[] args) { //arguments => expresion //No arguments, () => ... //one arguments we can avoid using parenthesis, x => ... //More than one arguments we have to use parenthesis, (x,y) => ... Func <int, int> square = number => number * number; Console.WriteLine(square(5)); //Scope const int factor = 5; Func <int, int> multipler = n => n * factor; int result = multipler(10); //Way 1 - Using Predicate functions var books = new BookRepository().GetBooks(); var cheapBooks1 = books.FindAll(IsCheaperThan10); foreach (var book in cheapBooks1) { Console.WriteLine(book.Title); } //Way 2 - Using lambda expression and LINQ BookRepository bookRepository = new BookRepository(); List <Book> cheapBooks2 = (bookRepository.GetBooks()).Where(book => book.Price < 10).ToList(); Console.WriteLine("--------------------"); foreach (var book in cheapBooks2) { Console.WriteLine(book.Title); } //Way 3 - Using Lambda expression var books3 = new BookRepository().GetBooks(); var cheapBooks3 = books3.FindAll(b => b.Price < 10); Console.WriteLine("--------------------"); foreach (var book in cheapBooks3) { Console.WriteLine(book.Title); } }
public static void Main() { // Making use of the private constant defined earlier. //Func<int, int> square = n => n * _factor; //Console.WriteLine(square(5)); var repository = new BookRepository(); // 1. Without Lambda expressions var books = repository.GetBooks(); // Call a predicate to return the books // var cheapbooks = books.FindAll(FindCheapBooks); // Add a Lambda expression instead var cheapbooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapbooks) { Console.WriteLine(book.Title); } }
// static void Main(string[] args) // { // // args => expression // int factor = 5; // // Func<int, int> square = number => number * number; // Func<int, int> multiple = number => factor * number; // square = Square; // Console.WriteLine(square(10)); // Console.WriteLine(multiple(45)); // } static void Main(String[] args) { var books = BookRepository.GetBooks(); // var cheapBooks = books.FindAll(IsCheaperThan10Dollars); // var cheapBooks = books.FindAll(book => book.Price < 10); // foreach (var book in cheapBooks) // { // Console.WriteLine(book.Title + " " + book.Price); // } // linq expressions var cheapBooks = books .Where(b => b.Price < 10) .OrderBy(book => book.Title) .Select(book => book.Title); var cheapBooksOperator = from b in books where (b.Price < 10) orderby(b.Title) select(b.Title); var booksOrderedByTitle = books.OrderBy(book => book.Title); }