现在的位置: 首页 > 综合 > 正文

LINQ入门语法

2013年04月08日 ⁄ 综合 ⁄ 共 881字 ⁄ 字号 评论关闭

 

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();         
        }

抱歉!评论已关闭.