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

ExpandedBlockStart.gif ContractedBlock.gif /**/ ///
None.gif / Take,Skip,TakeWhile,SkipWhile的例子和解释 //
ExpandedBlockStart.gifContractedBlock.gif
/**/ //
None.gif
None.gif
class  Program
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DB#region DB
InBlock.gif
InBlock.gif        
private static List<Student> GetStudents()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            List
<Student> students = new List<Student> dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="YOUNG", Age=25, Language="Chinese"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="JESSIE", Age=21, Language="Scotland"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="KELLY", Age=18, Language="English"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="JUNE", Age=20, Language="Chinese"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="ADRIAN", Age=22, Language="Italy"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BRUCE", Age=17, Language="Scotland"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BRANT", Age=30, Language="Germany"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BEN", Age=25, Language="Chinese"}
ExpandedSubBlockEnd.gif             }
;
InBlock.gif            
return students;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List
<Student> studentList = GetStudents();            
InBlock.gif            
InBlock.gif            
//Take(studentList);
InBlock.gif
InBlock.gif            
//Skip(studentList);
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Take#region Take
InBlock.gif
InBlock.gif        
public static void Take<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//GetElementFromCollect();
InBlock.gif            
//GetElementFromSelectResult(obj);
InBlock.gif            
//GetPointElement(obj);
InBlock.gif
InBlock.gif            
//ForExampleAboutTakeWhileOrSkipWhile(obj);
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static void GetElementFromCollect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>从集合中取固定数量的元素:<br>");
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] stringArray = dot.gif"Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };
InBlock.gif
InBlock.gif            
//Take将枚举出source中的前count个元素,返回给客户端.
InBlock.gif            
//Lambda的写法   
InBlock.gif            
//var books = stringArray.Take(3);
InBlock.gif
InBlock.gif            
//Linq的写法
InBlock.gif
            var books = (from n in stringArray
InBlock.gif                        select n).Take(
3);
InBlock.gif            
foreach (var book in books)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", book));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//扩展方法写法
InBlock.gif            
//IEnumerator<String> stu_Lambda = stringArray.Take(3).GetEnumerator();
InBlock.gif
InBlock.gif            
//while (stu_Lambda.MoveNext())
InBlock.gif            
//{
InBlock.gif            
//    Console.WriteLine(stu_Lambda.Current);
InBlock.gif            
//}
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static void GetElementFromSelectResult<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>从select结果中取固定数量的元素:<br>");
InBlock.gif
InBlock.gif            
//Lambda写法
InBlock.gif            
//var stu = (RevealChangeType(obj).Where(n=>n.Language == "Chinese").Select(n=>new { n.Name, n.Age })).Take(1);
InBlock.gif
InBlock.gif            
//Linq写法
InBlock.gif
            var stu = (from n in RevealChangeType(obj)
InBlock.gif                       where n.Language 
== "Chinese"
ExpandedSubBlockStart.gifContractedSubBlock.gif                       select 
new dot.gif{n.Name, n.Age}).Take(1);
InBlock.gif
InBlock.gif            
foreach (var student in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'> name:{0}; age:{1}</div>", student.Name, student.Age));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//扩展方法写法
InBlock.gif            
//IEnumerator<Student> stu_ExtraMethod = RevealChangeType(obj).Where(n=>n.Language == "Chinese").Take(1).GetEnumerator();
InBlock.gif
InBlock.gif            
//while (stu_ExtraMethod.MoveNext())
InBlock.gif            
//{
InBlock.gif            
//    Console.WriteLine(stu_ExtraMethod.Current.Age);
InBlock.gif            
//}
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static void GetPointElement<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>取满足指定条件元素:<br>");
InBlock.gif
InBlock.gif            
//Lambda写法
InBlock.gif            
//var stu = RevealChangeType(obj).TakeWhile(student => student.Age > 20);
InBlock.gif
InBlock.gif            
//Linq写法
InBlock.gif
            var stu = (from n in RevealChangeType(obj)
InBlock.gif                       select n).TakeWhile(n 
=> n.Age > 20);
InBlock.gif
InBlock.gif            
foreach (var student in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", student.Name));
ExpandedSubBlockEnd.gif            }

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

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Skip#region Skip
InBlock.gif
InBlock.gif        
private static void Skip<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//GetRemnantElementAfterJumpCollect();
InBlock.gif            
//GetRemnantElementAfterFilter(obj);
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static void GetRemnantElementAfterJumpCollect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>跳过集合的几个元素后取剩余所有元素:<br>");
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] stringArray = dot.gif"Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };
InBlock.gif
InBlock.gif            
//Skip则恰好相反,将跳过source中的前count个元素,枚举其余元素.
InBlock.gif
InBlock.gif            
//Lambda写法
InBlock.gif            
//var books1 = stringArray.Skip(3);
InBlock.gif
InBlock.gif            
//Linq写法
InBlock.gif
            var books1 = (from n in stringArray
InBlock.gif                         select n).Skip(
3);
InBlock.gif
InBlock.gif            
foreach (var book in books1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", book));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//扩展方法写法
InBlock.gif            
//IEnumerator<string> books1_ExtraMethod = stringArray.Skip(3).GetEnumerator();
InBlock.gif
InBlock.gif            
//while (books1_ExtraMethod.MoveNext())
InBlock.gif            
//{
InBlock.gif            
//    Console.WriteLine(books1_ExtraMethod.Current);
InBlock.gif            
//}
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static void GetRemnantElementAfterFilter<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>将满足指定条件元素过滤后选择剩余的所有元素:<br>");
InBlock.gif
InBlock.gif            
//Lambda写法
InBlock.gif            
//var stu = RevealChangeType(obj).SkipWhile(student => student.Age > 20);
InBlock.gif             
InBlock.gif            
//Linq写法
InBlock.gif
            var stu = (from n in RevealChangeType(obj)
InBlock.gif                      select n).SkipWhile(n 
=> n.Age > 20);
InBlock.gif
InBlock.gif            
foreach (var student in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", student.Name));
ExpandedSubBlockEnd.gif            }

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

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
TakeWhile--SkipWhile#region TakeWhile--SkipWhile
InBlock.gif
InBlock.gif        
private static void ForExampleAboutTakeWhileOrSkipWhile<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            var stu 
= RevealChangeType(obj).TakeWhile(student => student.Age > 20);
InBlock.gif            var stu1 
= RevealChangeType(obj).TakeWhile(student => student.Age > 18);
InBlock.gif            var stu2 
= RevealChangeType(obj).SkipWhile(student => student.Age > 20);
InBlock.gif            var stu3 
= RevealChangeType(obj).SkipWhile(student => student.Age > 18);
InBlock.gif
InBlock.gif            
foreach (var student in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='[TakeWhile(student => student.Age > 20)]result'>{0}</div>", student.Name));
ExpandedSubBlockEnd.gif            }

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

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

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

InBlock.gif
InBlock.gif            
//Runtime Result
InBlock.gif            
//<div class='[TakeWhile(student => student.Age > 20)]result'>YOUNG</div>
InBlock.gif            
//<div class='[TakeWhile(student => student.Age > 20)]result'>JESSIE</div>
InBlock.gif
InBlock.gif            
//<div class='[TakeWhile(student => student.Age > 18)]result'>YOUNG</div>
InBlock.gif            
//<div class='[TakeWhile(student => student.Age > 18)]result'>JESSIE</div>
InBlock.gif
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 20)]result'>KELLY</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 20)]result'>JUNE</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 20)]result'>ADRIAN</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 20)]result'>BRUCE</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 20)]result'>BRANT</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 20)]result'>BEN</div>
InBlock.gif
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 18)]result'>KELLY</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 18)]result'>JUNE</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 18)]result'>ADRIAN</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 18)]result'>BRUCE</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 18)]result'>BRANT</div>
InBlock.gif            
//<div class='[SkipWhile(student => student.Age > 18)]result'>BEN</div>
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif
None.gif    
sealed   class  Student
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public int age;
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn age; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ age = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string name;
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string language;
InBlock.gif        
public string Language
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn language; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ language = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

        对于predicate的解释和实际用法,在我的blogs中--》 查找List,Array中元素的其他C#写法 中都有文字介绍和例子的演示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值