LINQ入门语法

 

Code

 

class IntroToLINQ
{       
    static void Main()
    {
        // The Three Parts of a LINQ Query:
        //  1. Data source.
        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

        // 2. Query creation.
        // numQuery is an IEnumerable<int>
        var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

        // 3. Query execution.
        foreach (int num in numQuery)
        {
            Console.Write("{0,1} ", num);
        }
    }
}

 


将查询出来的数据添加到一个集合

 
static void Main(string[] args)
        {     
// The Three Parts of a LINQ Query:
            
//  1. Data source.
            int[] numbers = new int[7] { 0123456 };

            List
<int> numQuery2 = (from num in numbers where (num % 2== 0 select num).ToList();

            
foreach (int num in numQuery2)
            {
                Console.WriteLine(
"{0,1}", num);
            }

           Console.ReadLine();
         
        }

posted @ 2008-11-28 15:42  paymob  阅读(226)  评论(0编辑  收藏  举报