LINQ语句的两种语法实现方式

文章转自:https://blog.csdn.net/lucky51222/article/details/45648805
using System;  
using System.Linq;  
  
namespace LINQ语法实现  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            int[] a = { 3,1,2,4};  
  
            //1.Query syntax  
            var Query1 = from num in a   
                         where num % 2 == 0   
                         orderby num   
                         select num;  
            foreach (var i in Query1)  
            {  
                Console.WriteLine("{0}", i);  
            }  
            Console.WriteLine();  
  
            //2.Method syntax  
            var Query2 = a.Where(n => n % 2 == 0).OrderBy(n => n);  
            foreach (var j in Query2)  
            {  
                Console.WriteLine("{0}", j);  
            }  
        }  
    }  
}  

posted @ 2018-06-01 11:53  王雪亮  阅读(437)  评论(0编辑  收藏  举报