光脚丫学LINQ(013):LINQ查询语法与方法语法
时间:2010-10-29 来源:光脚丫思考
static void Main(string[] args) { int[] Numbers = { 5, 10, 8, 3, 6, 12 }; // Query syntax: IEnumerable<int> NumQuery1 = from num in Numbers where num % 2 == 0 orderby num select num; // Method syntax: IEnumerable<int> NumQuery2 = Numbers.Where(num => num % 2 == 0).OrderBy(n => n); foreach (int i in NumQuery1) { Console.Write(i + " "); } Console.WriteLine(Environment.NewLine); foreach (int i in NumQuery2) { Console.Write(i + " "); } // Keep the console open in debug mode. Console.WriteLine(Environment.NewLine); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } static void Main(string[] args) { int[] Numbers = { 5, 10, 8, 3, 6, 12 }; // Query syntax: IEnumerable<int> NumQuery1 = from num in Numbers where num % 2 == 0 orderby num select num; // Method syntax: IEnumerable<int> NumQuery2 = Numbers.Where(num => num % 2 == 0).OrderBy(n => n); foreach (int i in NumQuery1) { Console.Write(i + " "); } Console.WriteLine(Environment.NewLine); foreach (int i in NumQuery2) { Console.Write(i + " "); } // Keep the console open in debug mode. Console.WriteLine(Environment.NewLine); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
两个示例的输出是相同的。您可以看到两种形式的查询变量的类型是相同的:IEnumerable<T>。
相关阅读 更多 +