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
万峰图片批量处理专家,界面简洁易用,功能强大实用.支持多种处理任务同时按顺序执行,真正的批量图片,批量效果处理.支持图片批量自定义的放大缩小,旋转或者翻转,支持图片格式批量转换.支持图片批量文字水印, ...
随机推荐
- 超出div宽度范围的文字进行省略号省略,在鼠标移上去以后显示完整的内容
一.前言 当我们在固定的范围内显示内容时,我们是希望能够完整显示的,然而往往事与愿违,文本会超出我们给定的范围,这时候怎么办呢? 二.超出范围,对文本进行省略号隐藏 先上图 代码很简单 div{ wi ...
- CloudStack + KVM + HA
KVM高可用性CS4.2暂时没有实现 The Linux Kernel Virtual Machine (KVM) is a very popular hypervisor choice amongs ...
- Centos6.6升级python2到python3
系统更新部分: 一.由于系统原有的源无法连接,需要更新为新的源.起初,首选163的源,但是由于更改源以后,无法使用yum等问题,所以直接使用上海交通大学提供的源. 修改前,将原来/etc/yum.re ...
- 设计模式(java)--模版方法模式之任务分配
转自:http://blog.csdn.net/zhengzhb/article/details/7405608 定义:定义一个操作中算法的框架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构 ...
- sql查询层级分类
先上个效果图吧 CTE递归查询里面用了一些小的技巧,查询出结果以后在前端用表格展示出来,层级视觉效果还是很明显的 with tree as(select [ID],[Name],[Address],[ ...
- CentOS7 Failed to start LSB: Bring up/down
原文地址:http://addam.blog.51cto.com/5041993/1839518 刚刚装好的虚拟机突然不能上网了,报错很诡异,具体报错如下: /etc/init.d/network r ...
- windows10最实用的快捷键、高效的windows模式
win+方向键 将软件窗口放置到方向 ctrl+win+左右方向键 切换虚拟桌面 win+tab 选择窗口或虚拟桌面,然后按着win随后放开,界面停留 alt+tab 切换窗口 ctrl+win+D ...
- 使用#include消除重复代码
消除重复代码代码很多种,比如: 1)提炼成函数复用 2)使用宏 3)继承 4)使用闭包(boost::bind.boost::function) 上述是最为常用的,对于C++程序,闭包可能用得相对少一 ...
- Matlab图像处理教程
虽然典型算法的开发是基于理论支持的,但这些算法的实现几乎总是要求参数估计,并常常进行算法修正与候选求解方案的比较. MATLAB由LINPACK和EISPACK项目开发,最初用于矩阵处理.今天,MAT ...
- 洛谷P1501 [国家集训队]Tree II(打标记lct)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...