LINQ--language-integrated-query

1、它和sql语言区别呢?

SQL语言常用在ralational-database中,而LINQ对内存数据,数据库,xml文件等多种形式进行操作。简而言之,操作范围广。

2、下面我们简单地来实现一下这个基本的linq:

首先,创一个customer类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQ_Exam
{
    //create a simple Customer class
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string HomeAddress{ get; set; }
        //now override the ToString function of Object class.
        public override string ToString()
        {
            return string.Format("{0} {1}\n Enmail:{2}",FirstName,LastName,HomeAddress);
        }
        public static List<Customer> CreateCustomerList()
        {
            List<Customer> customers = new List<Customer>
            {
                 new Customer { FirstName = "Orlando",LastName = "Gee", HomeAddress = "orlando0@adventure-works.com"},
                 new Customer { FirstName = "Keith", LastName = "Harris",HomeAddress = "keith0@adventure-works.com" },
                 new Customer { FirstName = "Donna", LastName = "Carreras",HomeAddress = "donna0@adventure-works.com" },
                 new Customer { FirstName = "Janet", LastName = "Gates",HomeAddress = "janet1@adventure-works.com" },
                 new Customer { FirstName = "Lucy", LastName = "Harrington",HomeAddress = "lucy0@adventure-works.com" }
            };
            return customers;
        }
    }
}

然后,写几句话去用LINQ了,我在test类中main函数中去实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQ_Exam
{
    public class Tester
    {
        public static void Main()    
        {
            List<Customer> customers = Customer.CreateCustomerList();
            IEnumerable<Customer> result = from customer in customers
                                           where customer.FirstName == "Donna"
                                           select customer;
            Console.WriteLine("FirstName = \"Donna\"");
            foreach(Customer customer in result)
            {
                Console.WriteLine(customer.ToString());
            }
            customers[3].FirstName = "Donna";
             Console.WriteLine("FirstName == \"Donna\" (take two)");
             foreach (Customer customer in result)
                {  Console.WriteLine(customer.ToString());}
          }
       }
}
然后执行结果为:

可见,linq好似和sql的用法差不多,只是要记住其范围可用更加广阔。

3、在这小例子中,我们需要注意的问题:

*

from   customer  in customers  这里的customer其实是一个变量名称,它迭代了customers里面的每一个数值,就相当于吧customers里面的

每一个值去赋予到customer中,然后进行筛选,为真的则保留下来,然后得到最后的查找结果。

*

Deferred(延期的) Query Evaluation

IEnumerable<Customer> result = from   customer  in customers

where  customer.FirstName == "Donna“   select  customer;

这个是定义一个查找结果的代码,当代码写出来的时候,代码并没有运行产生一个result,此时仅仅是定义一个result的变量,这个变量里还没有任何数据。

因为下面的LINQ语句并没有执行。然而多会执行了呢?-------------------当result需要被用到的时候,就被执行了,就相当于result现场被填充了内容。

如:

foreach (Customer customer in result) {………}这个语句用到了result,然后result就会被立马填充内容了

这个很类似于sql中的view(视图)视图是空的,显示视图的内容只是进行一个对表的查找,然后再显示出来。就是这样啦。

ps:如果你不想让它这么干,可以这么写:(用到了ToList或者ToArray)

ToList<Customer> result = from   customer  in customers

where  customer.FirstName == "Donna“   select  customer;

LINQ -2015-04-27的更多相关文章

  1. 2015.04.27,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 35

    1.how to look 拉丁词根specto,to look的意思,是许多常见英文词语的来源,如spectacle(['spektәkl] n. 值得看的东西, 光景, 眼镜).spectator ...

  2. Contest2073 - 湖南多校对抗赛(2015.04.06)

    Contest2073 - 湖南多校对抗赛(2015.04.06) Problem A: (More) Multiplication Time Limit: 1 Sec  Memory Limit:  ...

  3. 关于asp.net执行exe程序时权限不够的解决办法(2015.04.17更新)

    一,本文背景 长话短说:asp.net项目中需要用到PDF转换成SWF文件,用户上传后自动调用pdf2swf.exe转换. 但有个问题,执行时权限不够,导致一直报错(滚动条一直在往下滚,刷屏中),见下 ...

  4. http://deepdish.io/2015/04/28/creating-lmdb-in-python/

    http://deepdish.io/2015/04/28/creating-lmdb-in-python/

  5. 2015.04.15,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯” SESSION 27

    继续学习交谈习惯的单词,本大章节完成. 1. front and back - and uncles ventriloquist,从belly发声(venter, venris + loquor). ...

  6. JS 2016-09-30T22:04:27.5220743+08:00 转换为日期

    1.转换代码 new Date(item.CreatedDate).Format("yyyy-MM-dd hh:mm") 2.需要拓展的方法 // 对Date的扩展,将 Date ...

  7. voa 2015 / 4 / 27

    As reports of the death toll rise in Nepal, countries and relief organizations around the world are ...

  8. C#3.0新增功能09 LINQ 基础04 基本 LINQ 查询操作

    连载目录    [已更新最新开发文章,点击查看详细] 本篇介绍 LINQ 查询表达式和一些在查询中执行的典型操作. 获取数据源 在 LINQ 查询中,第一步是指定数据源. 和大多数编程语言相同,在使用 ...

  9. Week 1:2015/4/27~2015/5/3

    Update everyday.(Last edit:4/30 01:00) Task 1:TPO X 2.5(finish 1,then finish 2 more) Task 2:TC Tarja ...

  10. [2015.07.27]万峰图片批量处理专家 v8.6

    万峰图片批量处理专家,界面简洁易用,功能强大实用.支持多种处理任务同时按顺序执行,真正的批量图片,批量效果处理.支持图片批量自定义的放大缩小,旋转或者翻转,支持图片格式批量转换.支持图片批量文字水印, ...

随机推荐

  1. 高性能Web服务器Nginx的配置与部署研究(4)Nginx常用命令

    1. 启动 Nginx poechant@ubuntu:sudo ./sbin/nginx 2. 停止 Nginx poechant@ubuntu:sudo ./sbin/nginx -s stop ...

  2. 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值

    [抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  3. loadrunner添加load generator连接失败解决办法

    1.到防火墙设置里面“允许程序和功能通过windows防火墙”,然后添加Loadrunner Agent Procss,到列表中,在“专用”和“公用”打勾,然后重启一下LR和Loadrunner Ag ...

  4. 洛谷 P3478 [POI2008]STA-Station

    题目描述 The first stage of train system reform (that has been described in the problem Railways of the ...

  5. myeclipse快捷键记忆

    提示 Alt+?自动排版 Ctrl+shift+f自动添加引入包 Ctrl+shift+O切换窗口 Ctrl+F6自动添加set get方法 Alt+shift+s r 查看都是哪里调用了该方法 Ct ...

  6. Ubuntu14.04-LTS 从系统安装到配置可用

    1.安装Ubuntu14.04LTS-64bit 使用U盘安装很方便快捷,可以使用老毛桃使用iso模式制作一个U盘启动盘,然后分区安装. 如果使用硬盘安装的话需要注意的问题是: 如果电脑上以前有Lin ...

  7. CRISPR/Cas9基因敲除原理及实验建议

    CRISPR/Cas9基因敲除原理及实验建议   CRISPR Cas9已经成为了最受欢迎的基因编辑技术之一,在2016年的国自然基金中也有很多项目是关于 CRISPR Cas9的.目前在市场上已经有 ...

  8. nignx ssl 配置

    1. 找一个目录,例如: usr/local/nginx/ssl ,进入该目录 2.  openssl genrsa -des3 -out server.key 1024  创建自身秘钥 3.  op ...

  9. [转][译] 存储引擎原理:LSM

    原译文地址:http://www.tuicool.com/articles/qqQV7za http://www.zhihu.com/question/19887265 http://blog.csd ...

  10. Spark 0.9.1和Shark 0.9.1分布式安装指南

    目录 目录 1 1. 约定 1 2. 安装Scala 1 2.1. 下载 2 2.2. 安装 2 2.3. 设置环境变量 2 3. 安装Spark 2 3.1. 部署 2 3.2. 下载 3 3.3. ...