Linq详解
class Program { static void Main(string[] args) { string[] currentVideoGames = {"Morrowing","BloShock","Half life 2","Episode 1","The Darkness","Daxter" }; var subset = from q in currentVideoGames where q.Length > 6 orderby q select q; //Linq实际还是委托,可以用Enumerable和Lambda表达式来建立查询表达式 //其中一个where的原型是public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); var labdSubset = currentVideoGames.Where(game => game.Length > 6).OrderBy(game => game).Select(game => game); //Enumerable.where()方法利用了System.Fun<To,TResult>委托型。 //第一参数:要处理的于IEumerable<T>兼容的数据 //第二个参数:表示要处理的方法,这个地方用lambda //扩展方法可以在实例层次调用,也可以在定义的静态成员来调用,所以上面的表达式也可以换成下面的。 //public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable //System.Array没有直接实现IEnumberable<T>接口,从上面的定义可以看出,但是他通过静态的Enumerable类间接的得到了所需功能,通过扩展方法 //public static class Enumerable var labdaSubset2 = Enumerable.Where(currentVideoGames, game => game.Length > 6).OrderBy(game => game).Select(game => game); ///Func<T.s>所需要的匿名方法 Func<string,bool> searchFile=delegate(string msg){return msg.Length>6;}; Func<string,string> itemToProcess=delegate(string msg){return msg}; var varMeth = currentVideoGames.Where(searchFile).OrderBy(itemToProcess).Select(itemToProcess); ///用最原始的实现方法 Func<string,bool> searchfile1=new Func<string,bool>(fileter); } static bool fileter(string msg) { return msg.Length > 6; } }
仔细看完会有收获的