问题描述
xxx.Where((aa,bb)=>aa.Length<bb);明白以上代码是什么意思?(大家都是谈初学,高手浏览即可)1、Where子句其实是用扩展方法来实现的MS实现的Where子句对应的扩展函数是如下的定义:namespaceSystem.Linq{publicdelegateTResultFunc<TArg0,TArg1,TResult>(TArg0arg0,TArg1arg1);publicstaticclassEnumerable{publicstaticIEnumerable<TSource>Where<TSource>(thisIEnumerable<TSource>source,Func<TSource,bool>predicate);publicstaticIEnumerable<TSource>Where<TSource>(thisIEnumerable<TSource>source,Func<TSource,int,bool>predicate);}}其中红色字体的那个扩展函数,就是我们上面代码实际使用的扩展函数。我们这个扩展函数参数:Func<TSource,int,bool>predicate的定义看上面代码的绿色delegate代码。2、Where子句参数书写的是Lambda表达式((aa,bb)=>aa.Length<bb);就相当于C#2.0的匿名函数。LINQ中所有关键字比如Select,SelectMany,Count,All等等其实都是用扩展方法来实现的。上面的用法同样也适用于这些关键字子句。3、这个Where子句中Lambda表达式第二个参数是数组索引,我们可以在Lambda表达式内部使用数组索引。来做一些复杂的判断。具有数组索引的LINQ关键字除了Where还以下几个Select,SelectMany,Count,All我们下面就来依次举例Select子句使用数组索引的例子下面代码有一个整数数组,我们找出这个数字是否跟他在这个数组的位置一样publicstaticvoidLinqDemo01(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varnumsInPlace=numbers.Select((num,index)=>new{Num=num,InPlace=(num==index)});Console.WriteLine("Number:In-place?");foreach(varninnumsInPlace)Console.WriteLine("{0}:{1}",n.Num,n.InPlace);}输出结果:Number:In-place?5:False4:False1:False3:True9:False8:False6:True7:True2:False0:False其中我们用到的这个Select子句对应的扩展函数定义,以及其中Func<TSource,int,TResult>委托定义如下:publicstaticIEnumerable<TResult>Select<TSource,TResult>(thisIEnumerable<TSource>source,Func<TSource,int,TResult>selector);publicdelegateTResultFunc<TArg0,TArg1,TResult>(TArg0arg0,TArg1arg1);SelectMany子句使用数组索引的例子几个句子组成的数组,我们希望把这几个句子拆分成单词,并显示每个单词在那个句子中。查询语句如下:publicstaticvoidDemo01(){string[]text={"Albertwashere","Burkesleptlate","Connorishappy"};vartt=text.SelectMany((s,index)=>fromssins.Split('')selectnew{Word=ss,Index=index});foreach(varnintt)Console.WriteLine("{0}:{1}",n.Word,n.Index);}结果:Albert:0was:0here:0Burke:1slept:1late:1Connor:2is:2happy:2SkipWhile子句使用数组索引的例子SkipWhile意思是一直跳过数据,一直到满足表达式的项时,才开始返回数据,而不管之后的项是否仍然满足表达式,需要注意他跟Where是不一样的,Where是满足条件的记录才返回,SkipWhile是找到一个满足条件的,然后后面的数据全部返回。下面例子返回一个整数数组中,这个整数比他自身在这个数组的位置大于等于的第一个位置以及之后的数据。publicstaticvoidLinq27(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varlaterNumbers=numbers.SkipWhile((n,index)=>n>=index);Console.WriteLine("Allelementsstartingfromfirstelementlessthanitsposition:");foreach(varninlaterNumbers)Console.WriteLine(n);}输出结果:Allelementsstartingfromfirstelementlessthanitsposition:13986720First、FirstOrDefault、Any、All、Count子句注意:101LINQSamples中First-Indexed、FirstOrDefault-Indexed、Any-Indexed、All-Indexed、Count-Indexed这五个例子在OrcasBeta1中已经不在可用,即下面代码是错误的。publicvoidLinq60(){int[]numbers={5,4,1,3,9,8,6,7,2,0};intevenNum=numbers.First((num,index)=>(num%2==0)&&(index%2==0));Console.WriteLine("{0}isanevennumberatanevenpositionwithinthelist.",evenNum);}publicvoidLinq63(){double?[]doubles={1.7,2.3,4.1,1.9,2.9};double?num=doubles.FirstOrDefault((n,index)=>(n>=index-0.5&&n<=index+0.5));if(num!=null)Console.WriteLine("Thevalue{1}iswithin0.5ofitsindexposition.",num);elseConsole.WriteLine("Thereisnonumberwithin0.5ofitsindexposition.",num);}publicvoidLinq68(){int[]numbers={-9,-4,-8,-3,-5,-2,-1,-6,-7};boolnegativeMatch=numbers.Any((n,index)=>n==-index);Console.WriteLine("Thereisanumberthatisthenegativeofitsindex:{0}",negativeMatch);}publicvoidLinq71(){int[]lowNumbers={1,11,3,19,41,65,19};int[]highNumbers={7,19,42,22,45,79,24};boolallLower=lowNumbers.All((num,index)=>num<highNumbers[index]);Console.WriteLine("Eachnumberinthefirstlistislowerthanitscounterpartinthesecondlist:{0}",allLower);}publicvoidLinq75(){int[]numbers={5,4,1,3,9,8,6,7,2,0};intoddEvenMatches=numbers.Count((n,index)=>n%2==index%2);Console.WriteLine("Thereare{0}numbersinthelistwhoseodd/evenstatus"+"matchesthatoftheirposition.",oddEvenMatches);}要实现这个功能,可以用Where子句,如下:publicstaticvoidLinq60(){int[]numbers={5,4,1,3,9,8,6,7,2,0};intevenNum=numbers.Where((num,index)=>(num%2==0&&index%2==0)).First();Console.WriteLine("{0}isanevennumberatanevenpositionwithinthelist.",evenNum);}publicstaticvoidLinq63(){double?[]doubles={1.7,2.3,4.1,1.9,2.9};double?num=doubles.Where((n,index)=>(n>=index-0.5&&n<=index+0.5)).FirstOrDefault();if(num!=null)Console.WriteLine("Thevalue{1}iswithin0.5ofitsindexposition.",num);elseConsole.WriteLine("Thereisnonumberwithin0.5ofitsindexposition.",num);}publicstaticvoidLinq68(){int[]numbers={-9,-4,-8,-3,-5,-2,-1,-6,-7};boolnegativeMatch=numbers.Where((n,index)=>n==-index).Any();Console.WriteLine("Thereisanumberthatisthenegativeofitsindex:{0}",negativeMatch);}publicstaticvoidLinq71(){int[]lowNumbers={1,11,3,19,41,65,19};int[]highNumbers={7,19,42,22,45,79,24};boolallLower=lowNumbers.Where((num,index)=>num<highNumbers[index]).All(n=>true);Console.WriteLine("Eachnumberinthefirstlistislowerthanitscounterpartinthesecondlist:{0}",allLower);}publicstaticvoidLinq75(){int[]numbers={5,4,1,3,9,8,6,7,2,0};intoddEvenMatches=numbers.Where((n,index)=>n%2==index%2).Count();Console.WriteLine("Thereare{0}numbersinthelistwhoseodd/evenstatus"+"matchesthatoftheirposition.",oddEvenMatches);}转自:http://www.cnblogs.com/RuiLei/archive/2007/07/17/820507.html
解决方案
解决方案二:
学习了
解决方案三:
樓主,不錯
解决方案四:
顶mark
解决方案五:
jf,学习
解决方案六:
mark
解决方案七:
扩展函数的确很好很强大...
解决方案八:
该回复于2008-03-16 10:34:01被版主删除
解决方案九:
扩展函数的确很好很强大...
解决方案十:
jf&&up
解决方案十一:
扩展函数充分发挥Linq功能.感谢楼主分享!
解决方案十二:
引用10楼lt5225262的回复:
扩展函数充分发挥Linq功能.感谢楼主分享!