USE lambda expression

 

        static void Main(string[] args)
        {
            #region "Building a List of Customers"
 
            List<Customer> custList = new List<Customer>()
                    {new Customer() 
                          { CustomerId = 1, 
                            FirstName="Bilbo", 
                            LastName = "Baggins", 
                             SalesTotal=10.0M,
                            EmailAddress = "[email protected]"
                            }, 
                    new Customer() 
                          { CustomerId = 2, 
                            FirstName="Frodo", 
                            LastName = "Baggins", 
                               SalesTotal=10.0M,
                            EmailAddress = "[email protected]"}, 
                    new Customer() 
                          { CustomerId = 3, 
                            FirstName="Samwise", 
                            LastName = "Gamgee", 
                               SalesTotal=10.0M,
                            EmailAddress = "[email protected]"}, 
                    new Customer() 
                          { CustomerId = 4, 
                            FirstName="Rosie", 
                            LastName = "Cotton", 
                               SalesTotal=10.0M,
                            EmailAddress = "[email protected]"}};
 
            #endregion
 
            #region "Lambda Expressions: Finding an Item in a Generic List"
            //way1:loop list and compare
            Customer foundCustomer = null;
            foreach (var c in custList)
            {
                if (c.CustomerId == 4)
                {
                    foundCustomer = c;
                    break;
                }
            }
            Console.WriteLine(foundCustomer);
            //way2 :LINQ
            foundCustomer = null;
            var query = from c in custList
                        where c.CustomerId == 4
                        select c;
            foundCustomer = query.FirstOrDefault();
            Console.WriteLine(foundCustomer);
 
            //way3: anoymous method
            foundCustomer = custList.Find(delegate(Customer c)
            {
                return c.CustomerId == 4;
            });
            //way4:lambda expression
            foundCustomer = custList.FirstOrDefault<Customer>(c => c.CustomerId == 4);
            foundCustomer = custList.FirstOrDefault<Customer>(c =>
            {
                return c.CustomerId == 4;
            });
 
            foundCustomer = custList.FirstOrDefault<Customer>((Customer c) => c.CustomerId == 4);
            foundCustomer = custList.FirstOrDefault<Customer>((Customer c) =>
            {
                return c.CustomerId == 4;
            });
            foundCustomer = custList.Find(c => c.CustomerId == 4);
 
            //Lambda Expressions: Predicate Delegates
            //[ a predicate is “a function which returns a Boolean value”.]
 
            var target = Array.Find<Customer>(custList.ToArray(), c => c.LastName.StartsWith("K"));
            //Lambda Expressions: Action Delegates
            //an action delegate encapsulates a method that performs an action and has no return value. It takes up to four parameters (and this number is increased in .NET 4.0
 
            custList.ForEach(c => Console.WriteLine(c));
 
            //Lambda Expressions: Func Delegates
            //A Func delegate encapsulates a method that returns a value. 
            //It takes up to four parameters (and this number is increased in .NET 4.0) plus the return value.
            var total = custList.Sum(c => c.SalesTotal);
 
            //Lambdas: Aggregating Strings
            /*The Select method first selects the email address for each customer. 
             * The Aggregate method then appends the email addresses together.
             * And this code correctly handles the '”;” so you don’t have to think about it*/
            IEnumerable<string> email = custList.Select(c => c.EmailAddress);
            string m = email.Aggregate((items,item)=>items+";" +item);
            string m2=custList.Select(c => c.EmailAddress)  .Aggregate((items, item) => items + "; " + item);
 
            //Lambda Expressions: Finding Differences in Two Lists
            /*
             * To compare items in two lists and find which items in one list are not in the other.
             * For example: {1, 2, 3}, {2, 3, 4}. {1} is only in the first list.
             * To display the contents of the resulting set.
             */
            List<int> list1 = new List<int>() { 1, 6, 8 };
            List<int> list2 = new List<int>() { 2, 6 };
            /*
             This is a Func delegate which returns an IEnumerable<int>. 
             * There are no arguments passed into the function, hence the empty parenthesis ().
             * The => is the lambda operator. The remaining code uses the Except method of the list to find the items 
             * not in the second list.
             */
            Func<IEnumerable<int>> exceptionFunction = () => list1.Except(list2);
            //The following lambda expression displays the resulting items
            /*
             * This one is an Action delegate that takes no parameters. 
             * It first takes the IEnumerable result from the exceptionFunction,
             * converts it to a list, then writes each item to the Debug window
             */
            Action displayList = () => exceptionFunction().ToList().ForEach(i => Console.WriteLine(i));
            displayList(); // Result 1,8
 
 
            Console.ReadLine();
 
 
 
            #endregion
        }
發佈了110 篇原創文章 · 獲贊 5 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章