LINQ入门

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值