Linq 入门系列 [Take,Skip,TakeWhile,SkipWhile]篇

///////////////////////////////////////////
/Take,Skip,TakeWhile,SkipWhile的例子和解释//
//////////////////////////////////////////

class Program
    
{
        
#region DB

        
private static List<Student> GetStudents()
        
{
            List
<Student> students = new List<Student> 
             
new Student{ Name="YOUNG", Age=25, Language="Chinese"},
             
new Student{ Name="JESSIE", Age=21, Language="Scotland"},
             
new Student{ Name="KELLY", Age=18, Language="English"},
             
new Student{ Name="JUNE", Age=20, Language="Chinese"},
             
new Student{ Name="ADRIAN", Age=22, Language="Italy"},
             
new Student{ Name="BRUCE", Age=17, Language="Scotland"},
             
new Student{ Name="BRANT", Age=30, Language="Germany"},
             
new Student{ Name="BEN", Age=25, Language="Chinese"}
             }
;
            
return students;
        }


        
#endregion


        
static void Main(string[] args)
        
{
            List
<Student> studentList = GetStudents();            
            
            
//Take(studentList);

            
//Skip(studentList);
        }


        
#region Take

        
public static void Take<T>(T obj)
        
{
            
//GetElementFromCollect();
            
//GetElementFromSelectResult(obj);
            
//GetPointElement(obj);

            
//ForExampleAboutTakeWhileOrSkipWhile(obj);
        }


        
private static void GetElementFromCollect()
        
{
            Console.WriteLine(
"<hr>从集合中取固定数量的元素:<br>");
            
string[] stringArray = "Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };

            
//Take将枚举出source中的前count个元素,返回给客户端.
            
//Lambda的写法   
            
//var books = stringArray.Take(3);

            
//Linq的写法
            var books = (from n in stringArray
                        select n).Take(
3);
            
foreach (var book in books)
            
{
                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", book));
            }


            
//扩展方法写法
            
//IEnumerator<String> stu_Lambda = stringArray.Take(3).GetEnumerator();

            
//while (stu_Lambda.MoveNext())
            
//{
            
//    Console.WriteLine(stu_Lambda.Current);
            
//}
        }


        
private static void GetElementFromSelectResult<T>(T obj)
        
{
            Console.WriteLine(
"<hr>从select结果中取固定数量的元素:<br>");

            
//Lambda写法
            
//var stu = (RevealChangeType(obj).Where(n=>n.Language == "Chinese").Select(n=>new { n.Name, n.Age })).Take(1);

            
//Linq写法
            var stu = (from n in RevealChangeType(obj)
                       where n.Language 
== "Chinese"
                       select 
new {n.Name, n.Age}).Take(1);

            
foreach (var student in stu)
            
{
                Console.WriteLine(
string.Format("<div class='result'> name:{0}; age:{1}</div>", student.Name, student.Age));
            }


            
//扩展方法写法
            
//IEnumerator<Student> stu_ExtraMethod = RevealChangeType(obj).Where(n=>n.Language == "Chinese").Take(1).GetEnumerator();

            
//while (stu_ExtraMethod.MoveNext())
            
//{
            
//    Console.WriteLine(stu_ExtraMethod.Current.Age);
            
//}
        }


        
private static void GetPointElement<T>(T obj)
        
{
            Console.WriteLine(
"<hr>取满足指定条件元素:<br>");

            
//Lambda写法
            
//var stu = RevealChangeType(obj).TakeWhile(student => student.Age > 20);

            
//Linq写法
            var stu = (from n in RevealChangeType(obj)
                       select n).TakeWhile(n 
=> n.Age > 20);

            
foreach (var student in stu)
            
{
                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", student.Name));
            }


            
//扩展方法写法
            
//IEnumerator<Student> stu_ExtraMethod = RevealChangeType(obj).TakeWhile(student => student.Age > 20).GetEnumerator();

            
//while (stu_ExtraMethod.MoveNext())
            
//{
            
//    Console.WriteLine(stu_ExtraMethod.Current.Name);
            
//}

            
//TakeWhile在LINQ中实现的思想是:
            
//对数据源进行枚举,从第一个枚举得到的元素开始-->
            
//                 调用客户端传入的predicate( c.Name == ""woodyN")-->
            
//                 如果这个predicate委托返回true的话,
            
//                 则将该元素作为Current元素返回给客户端,并且,继续进行相同的枚举,判断操作.
            
//                 但是,一旦predicate返回false的话,
            
//                 MoveNext()方法将会返回false,枚举就此打住,忽略剩下的所有元素.
            
//所以此方法出来的结果是:
            
//<div class='result'>YOUNG</div>
            
//<div class='result'>JESSIE</div>
            
//因为在KELLYAge=18不符合当前where条件,所以会这样。:)
        }


        
#endregion


        
#region Skip

        
private static void Skip<T>(T obj)
        
{
            
//GetRemnantElementAfterJumpCollect();
            
//GetRemnantElementAfterFilter(obj);
        }


        
private static void GetRemnantElementAfterJumpCollect()
        
{
            Console.WriteLine(
"<hr>跳过集合的几个元素后取剩余所有元素:<br>");
            
string[] stringArray = "Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };

            
//Skip则恰好相反,将跳过source中的前count个元素,枚举其余元素.

            
//Lambda写法
            
//var books1 = stringArray.Skip(3);

            
//Linq写法
            var books1 = (from n in stringArray
                         select n).Skip(
3);

            
foreach (var book in books1)
            
{
                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", book));
            }


            
//扩展方法写法
            
//IEnumerator<string> books1_ExtraMethod = stringArray.Skip(3).GetEnumerator();

            
//while (books1_ExtraMethod.MoveNext())
            
//{
            
//    Console.WriteLine(books1_ExtraMethod.Current);
            
//}
        }


        
private static void GetRemnantElementAfterFilter<T>(T obj)
        
{
            Console.WriteLine(
"<hr>将满足指定条件元素过滤后选择剩余的所有元素:<br>");

            
//Lambda写法
            
//var stu = RevealChangeType(obj).SkipWhile(student => student.Age > 20);
             
            
//Linq写法
            var stu = (from n in RevealChangeType(obj)
                      select n).SkipWhile(n 
=> n.Age > 20);

            
foreach (var student in stu)
            
{
                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", student.Name));
            }


            
//扩展方法写法
            
//IEnumerator<Student> stu_ExtraMethod = RevealChangeType(obj).SkipWhile(student => student.Age > 20).GetEnumerator();

            
//while (stu_ExtraMethod.MoveNext())
            
//{
            
//    Console.WriteLine(stu_ExtraMethod.Current);
            
//}

            
//类似的,SkipWhile也对数据源进行枚举,从第一个枚举得到的元素开始,
            
//调用客户端的predicate,如果返回true,则跳过该元素,继续进行枚举操作.
            
//但是,如果一旦predicate返回为false,则该元素以后的所有元素,都不会再调用predicate,
            
//而全部枚举给客户端.
            
//所以此方法出来的结果是:
            
//<div class='result'>KELLY</div>
            
//<div class='result'>JUNE</div>
            
//<div class='result'>ADRIAN</div>
            
//<div class='result'>BRUCE</div>
            
//<div class='result'>BRANT</div>
            
//<div class='result'>BEN</div>
            
//以上根据这个结果对于linq中TakeWhile,SkipWhile有点认识了。
            
//但是我还是建议实际使用时还是要小心使用。
        }


        
#endregion


        
#region TakeWhile--SkipWhile

        
private static void ForExampleAboutTakeWhileOrSkipWhile<T>(T obj)
        
{
            var stu 
= RevealChangeType(obj).TakeWhile(student => student.Age > 20);
            var stu1 
= RevealChangeType(obj).TakeWhile(student => student.Age > 18);
            var stu2 
= RevealChangeType(obj).SkipWhile(student => student.Age > 20);
            var stu3 
= RevealChangeType(obj).SkipWhile(student => student.Age > 18);

            
foreach (var student in stu)
            
{
                Console.WriteLine(
string.Format("<div class='[TakeWhile(student => student.Age > 20)]result'>{0}</div>", student.Name));
            }

            
foreach (var student in stu1)
            
{
                Console.WriteLine(
string.Format("<div class='[TakeWhile(student => student.Age > 18)]result'>{0}</div>", student.Name));
            }

            
foreach (var student in stu2)
            
{
                Console.WriteLine(
string.Format("<div class='[SkipWhile(student => student.Age > 20)]result'>{0}</div>", student.Name));
            }

            
foreach (var student in stu3)
            
{
                Console.WriteLine(
string.Format("<div class='[SkipWhile(student => student.Age > 18)]result'>{0}</div>", student.Name));
            }


            
//Runtime Result
            
//<div class='[TakeWhile(student => student.Age > 20)]result'>YOUNG</div>
            
//<div class='[TakeWhile(student => student.Age > 20)]result'>JESSIE</div>

            
//<div class='[TakeWhile(student => student.Age > 18)]result'>YOUNG</div>
            
//<div class='[TakeWhile(student => student.Age > 18)]result'>JESSIE</div>

            
//<div class='[SkipWhile(student => student.Age > 20)]result'>KELLY</div>
            
//<div class='[SkipWhile(student => student.Age > 20)]result'>JUNE</div>
            
//<div class='[SkipWhile(student => student.Age > 20)]result'>ADRIAN</div>
            
//<div class='[SkipWhile(student => student.Age > 20)]result'>BRUCE</div>
            
//<div class='[SkipWhile(student => student.Age > 20)]result'>BRANT</div>
            
//<div class='[SkipWhile(student => student.Age > 20)]result'>BEN</div>

            
//<div class='[SkipWhile(student => student.Age > 18)]result'>KELLY</div>
            
//<div class='[SkipWhile(student => student.Age > 18)]result'>JUNE</div>
            
//<div class='[SkipWhile(student => student.Age > 18)]result'>ADRIAN</div>
            
//<div class='[SkipWhile(student => student.Age > 18)]result'>BRUCE</div>
            
//<div class='[SkipWhile(student => student.Age > 18)]result'>BRANT</div>
            
//<div class='[SkipWhile(student => student.Age > 18)]result'>BEN</div>
        }


        
#endregion

    }


    
sealed class Student
    
{
        
public int age;
        
public int Age
        
{
            
get return age; }
            
set { age = value; }
        }


        
private string name;
        
public string Name
        
{
            
get return name; }
            
set { name = value; }
        }


        
private string language;
        
public string Language
        
{
            
get return language; }
            
set { language = value; }
        }

    }

posted on 2008-03-21 11:22  秋天  阅读(7639)  评论(1编辑  收藏  举报

导航