LINQ入门

来源:互联网 发布:淘宝转运到台湾 编辑:程序博客网 时间:2024/05/16 09:39
Linq查询的三个步骤

            在我们使用Linq来查询数据的时候我们都会按照这三个步骤来做,这是初学者应该记住
            1.创建数据源,这里的数据源可以是数组,集合,XML,SQL等数据库
            2.新建一个查询,
                如: from xxxx in xxxxx where xxx select;这一种结构,注意的是,必须以from开头,select结尾
            3.执行查询。在这里我们通常用到执行查询就是foreach来做,当然有for语句也可以,但后者在效率上没有前者好;

下面我们就用这个三个步骤来做一个示例:

using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 
 7 namespace LINQDemo
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int[] number = new int[7] { 1324567 };  //create Datasourse
14             var n = from num in number where (num % 2 == 0) select num; // create Query
15             foreach (int a in n)   //Query execution
16             {
17                 Console.WriteLine(a.ToString());
18             }
19         }
20     }
21 }

个人博客:http://blog.amtemai.com

原创粉丝点击