USE lambda expression
来源:互联网 发布:2016淘宝怎么刷信用 编辑:程序博客网 时间:2024/06/01 14:22
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 = "bb@hob.me" }, new Customer() { CustomerId = 2, FirstName="Frodo", LastName = "Baggins", SalesTotal=10.0M, EmailAddress = "fb@hob.me"}, new Customer() { CustomerId = 3, FirstName="Samwise", LastName = "Gamgee", SalesTotal=10.0M, EmailAddress = "sg@hob.me"}, new Customer() { CustomerId = 4, FirstName="Rosie", LastName = "Cotton", SalesTotal=10.0M, EmailAddress = "rc@hob.me"}}; #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 }
- USE lambda expression
- Use Lambda expression to iterate collection members
- c# Use a lambda expression to define an event handler
- Lambda Expression
- some lambda expression
- c++ 11 Lambda expression
- c# lambda expression
- lambda expression demo
- copy_if() with lambda expression
- Lambda Expression In Java
- Java Lambda Expression
- Java 8: Lambda Expression
- Python-Lambda Expression
- Use lambda in Ruby
- Java Lambda表达式(Java Lambda Expression)
- C# Lambda Expression概念浅析
- Delegate Events and Lambda expression
- lambda expression capture list demo
- was概要文件类型
- Serv-u 10.4 登陆消息乱码解决方案
- pl/sql developer 入门使用
- (转)C++中extern “C”含义深层探索
- 大端 小端
- USE lambda expression
- 理性地选择客户进行IT规划
- ruby 闭包
- Eclipse WTP Tutorials - Creating Bottom Up Web Service via Apache Axis2
- 关于sqlite中primary key可以输入null的说明
- 继续ant
- 使用perl-ldap修改a user's password in MS Active Directory
- 指针的学习
- serio总线/platform总线