LINQ -2015-04-27
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中,然后进行筛选,为真的则保留下来,然后得到最后的查找结果。
*
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的更多相关文章
- 2015.04.27,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 35
		1.how to look 拉丁词根specto,to look的意思,是许多常见英文词语的来源,如spectacle(['spektәkl] n. 值得看的东西, 光景, 眼镜).spectator ... 
- Contest2073 - 湖南多校对抗赛(2015.04.06)
		Contest2073 - 湖南多校对抗赛(2015.04.06) Problem A: (More) Multiplication Time Limit: 1 Sec Memory Limit: ... 
- 关于asp.net执行exe程序时权限不够的解决办法(2015.04.17更新)
		一,本文背景 长话短说:asp.net项目中需要用到PDF转换成SWF文件,用户上传后自动调用pdf2swf.exe转换. 但有个问题,执行时权限不够,导致一直报错(滚动条一直在往下滚,刷屏中),见下 ... 
- http://deepdish.io/2015/04/28/creating-lmdb-in-python/
		http://deepdish.io/2015/04/28/creating-lmdb-in-python/ 
- 2015.04.15,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯” SESSION 27
		继续学习交谈习惯的单词,本大章节完成. 1. front and back - and uncles ventriloquist,从belly发声(venter, venris + loquor). ... 
- JS  2016-09-30T22:04:27.5220743+08:00  转换为日期
		1.转换代码 new Date(item.CreatedDate).Format("yyyy-MM-dd hh:mm") 2.需要拓展的方法 // 对Date的扩展,将 Date ... 
- voa 2015 / 4 / 27
		As reports of the death toll rise in Nepal, countries and relief organizations around the world are ... 
- C#3.0新增功能09 LINQ 基础04 基本 LINQ 查询操作
		连载目录 [已更新最新开发文章,点击查看详细] 本篇介绍 LINQ 查询表达式和一些在查询中执行的典型操作. 获取数据源 在 LINQ 查询中,第一步是指定数据源. 和大多数编程语言相同,在使用 ... 
- 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 ... 
- [2015.07.27]万峰图片批量处理专家 v8.6
		万峰图片批量处理专家,界面简洁易用,功能强大实用.支持多种处理任务同时按顺序执行,真正的批量图片,批量效果处理.支持图片批量自定义的放大缩小,旋转或者翻转,支持图片格式批量转换.支持图片批量文字水印, ... 
随机推荐
- 141. Linked List Cycle (List; Two-Pointers)
			Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ... 
- 【总结整理】AI产品经理大会2017(转载)
			从企业大数据到企业 AI | 易观智慧院院长 李智 1.AI 不是目的,而是要了解 AI 是什么,真正意义上的强人工智能在前沿领域尚未取得突破,暂时只能在影视文学作品中去思考人机关系.机器人三定律在未 ... 
- ArcGIS js api三种查询功能
			转自https://blog.csdn.net/lovecarpenter/article/details/52669777 
- Ros学习——Cmakelists.txt文件解读
			1.过程 .Required CMake Version (cmake_minimum_required) //CMake 需要的版本 .Package Name (project()) //#定义工 ... 
- 为什么要用Android Studio?
			为什么要用Android Studio 本书节选自<Android Studio实用指南> 作者: 毕小朋 目前本书已上传到百度阅读,在百度中搜索[Anroid Studio实用指南]便可 ... 
- 数字图像处理:基于MATLAB的车牌识别项目                                                                                                         标签:               图像处理matlab算法                                            2017-06-24 09:17             98人阅读              评论(0)
			学过了数字图像处理,就进行一个综合性强的小项目来巩固一下知识吧.前阵子编写调试了一套基于MATLAB的车牌识别的项目的代码.今天又重新改进了一下代码,识别的效果好一点了,也精简了一些代码.这里没有使用 ... 
- 十万个为什么:现在还没发现“虚函数virtual”和多态性的优点,估计是因为我还没有编程序吧。
			十万个为什么:现在还没发现“虚函数virtual”和多态性的优点,估计是因为我还没有编程序吧. 
- Python监控日志程序-乾颐堂
			一个简易的日志监控的脚本,功能如下:1.windows环境2.当匹配日志关键字时会发出声音,匹配的关键字不同,播放的声音不同3.能做到实时响应 注意:是在win环境下哦 直接上代码吧 1 2 3 4 ... 
- Use formatter to format your JAVA code
			In order to make the codes looks unified and make it easy to understand, it's better to use the same ... 
- jdk、jre、jvm的区别联系
			jdk包括以下三个东西: D:\Program Files\jdk1.7.0_21\bin\javac.exe和java.exe D:\Program Files\jdk1.7.0_21\jre D: ... 
