Lambda表达式练习

      根据微软的示例代码,体验一下

1.准备测试数据

ExpandedBlockStart.gif      static   int [] numbers  =   new   int []  5413986720 } ;
ExpandedBlockStart.gif    
static   string [] strings  =   new   string []  "zero""one""two""three""four""five""six""seven""eight""nine"} ;
None.gif    
ExpandedBlockStart.gif    
class  Person  {
InBlock.gif      
public string Name;
InBlock.gif      
public int Level;
ExpandedBlockEnd.gif    }

None.gif    
ExpandedBlockStart.gif    
static  Person[] persons  =   new  Person[]  {
ExpandedSubBlockStart.gif        
new Person {Name="Matt", Level=3},
ExpandedSubBlockStart.gif        
new Person {Name="Luca", Level=3},
ExpandedSubBlockStart.gif        
new Person {Name="Jomo", Level=5},
ExpandedSubBlockStart.gif        
new Person {Name="Dinesh", Level=3},
ExpandedSubBlockStart.gif        
new Person {Name="Charlie", Level=3},
ExpandedSubBlockStart.gif        
new Person {Name="Mads", Level=3},
ExpandedSubBlockStart.gif        
new Person {Name="Anders", Level=9}
ExpandedBlockEnd.gif        }
;

2.过滤数据

ExpandedBlockStart.gif      public   static   void  Sample1()  {
InBlock.gif        
// use Where() to filter out elements matching a particular condition       
InBlock.gif
        IEnumerable<int> fnums = numbers.Where(n => n < 5);
InBlock.gif    
InBlock.gif        Console.WriteLine(
"Numbers < 5");
ExpandedSubBlockStart.gif        
foreach(int x in fnums) {
InBlock.gif            Console.WriteLine(x);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
  

3.匹配首个字母

ExpandedBlockStart.gif      public   static   void  Sample2()  {
InBlock.gif        
// use First() to find the one element matching a particular condition       
InBlock.gif
        string v = strings.First(s => s[0== 'o');
InBlock.gif    
InBlock.gif        Console.WriteLine(
"string starting with 'o': {0}", v);
ExpandedBlockEnd.gif    }
     

4.根据numbers排序
ExpandedBlockStart.gif      public   static   void  Sample3()  {
InBlock.gif     
InBlock.gif        
// use Select() to convert each element into a new value
InBlock.gif
        IEnumerable<string> snums = numbers.Select(n => strings[n]);
InBlock.gif        
InBlock.gif        Console.WriteLine(
"Numbers");
ExpandedSubBlockStart.gif        
foreach(string s in snums) {
InBlock.gif            Console.WriteLine(s);
ExpandedSubBlockEnd.gif        }
           
ExpandedBlockEnd.gif    }

5.匿名类型, 注意var关键字

None.gif      public   static   void  Sample4()
ExpandedBlockStart.gif    
{
InBlock.gif        
// use Anonymous Type constructors to construct multi-valued results on the fly
ExpandedSubBlockStart.gif
        var q = strings.Select(s => new {Head = s.Substring(0,1), Tail = s.Substring(1)});
ExpandedSubBlockStart.gif        
foreach(var p in q) {
InBlock.gif            Console.WriteLine(
"Head = {0}, Tail = {1}", p.Head, p.Tail);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

6.联合查询(即使用两个以上的查询条件)

ExpandedBlockStart.gif      public   static   void  Sample5() 
InBlock.gif        
// Combine Select() and Where() to make a complete query
InBlock.gif
        var q = numbers.Where(n => n < 5).Select(n => strings[n]);
InBlock.gif        
InBlock.gif        Console.WriteLine(
"Numbers < 5");
ExpandedSubBlockStart.gif        
foreach(var x in q) {
InBlock.gif            Console.WriteLine(x);
ExpandedSubBlockEnd.gif        }
       
ExpandedBlockEnd.gif    }

7.使用ToList方法缓存数据,不知道这样说对不对

ExpandedBlockStart.gif      public   static   void  Sample6()  {
InBlock.gif        
// Sequence operators form first-class queries are not executed until you enumerate them.
InBlock.gif
        int i = 0;
InBlock.gif        var q 
= numbers.Select(n => ++i);
InBlock.gif        
// Note, the local variable 'i' is not incremented until each element is evaluated (as a side-effect).
ExpandedSubBlockStart.gif
        foreach(var v in q) {
InBlock.gif          Console.WriteLine(
"v = {0}, i = {1}", v, i);          
ExpandedSubBlockEnd.gif        }

InBlock.gif        Console.WriteLine();
InBlock.gif        
InBlock.gif        
// Methods like ToList() cause the query to be executed immediately, caching the results
InBlock.gif
        int i2 = 0;
InBlock.gif        var q2 
= numbers.Select(n => ++i2).ToList();
InBlock.gif        
// The local variable i2 has already been fully incremented before we iterate the results
ExpandedSubBlockStart.gif
        foreach(var v in q2) {
InBlock.gif          Console.WriteLine(
"v = {0}, i2 = {1}", v, i2);
ExpandedSubBlockEnd.gif        }
        
ExpandedBlockEnd.gif    }

8.分组查询

ExpandedBlockStart.gif      public   static   void  Sample7()  {
InBlock.gif        
// use GroupBy() to construct group partitions out of similar elements
InBlock.gif
        var q = strings.GroupBy(s => s[0]); // <- group by first character of each string
InBlock.gif
        
ExpandedSubBlockStart.gif        
foreach(var g in q) {
InBlock.gif          Console.WriteLine(
"Group: {0}", g.Key);
ExpandedSubBlockStart.gif          
foreach(string v in g) {
InBlock.gif            Console.WriteLine(
"/tValue: {0}", v);
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

9.统计聚合

ExpandedBlockStart.gif      public   static   void  Sample8()  {
InBlock.gif        
// use GroupBy() and aggregates such as Count(), Min(), Max(), Sum(), Average() to compute values over a partition
ExpandedSubBlockStart.gif
        var q = strings.GroupBy(s => s[0]).Select(g => new {FirstChar = g.Key, Count = g.Count()});
InBlock.gif        
ExpandedSubBlockStart.gif        
foreach(var v in q) {
InBlock.gif          Console.WriteLine(
"There are {0} string(s) starting with the letter {1}", v.Count, v.FirstChar);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

10.排序

None.gif          //  use OrderBy()/OrderByDescending() to give order to your resulting sequence
None.gif
        var q  =  strings.OrderBy(s  =>  s);   //  order the strings by their name
None.gif
        
ExpandedBlockStart.gif        
foreach ( string  s  in  q)  {
InBlock.gif          Console.WriteLine(s);
ExpandedBlockEnd.gif        }

None.gif    }

11.二次排序

ExpandedBlockStart.gif      public   static   void  Sample9a()  {  
InBlock.gif        
// use ThenBy()/ThenByDescending() to provide additional ordering detail
InBlock.gif
        var q = persons.OrderBy(p => p.Level).ThenBy(p => p.Name);
InBlock.gif        
ExpandedSubBlockStart.gif        
foreach(var p in q) {
InBlock.gif          Console.WriteLine(
"{0}  {1}", p.Level, p.Name);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

你可能感兴趣的:(F.net,FrameWork)