﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-carternzj-随笔分类-Linq</title><link>http://www.cnitblog.com/carternzj/category/5847.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 29 Sep 2011 05:14:01 GMT</lastBuildDate><pubDate>Thu, 29 Sep 2011 05:14:01 GMT</pubDate><ttl>60</ttl><item><title>Linq学习笔记(1)</title><link>http://www.cnitblog.com/carternzj/archive/2007/07/31/hustnn.html</link><dc:creator>小小牛</dc:creator><author>小小牛</author><pubDate>Tue, 31 Jul 2007 15:26:00 GMT</pubDate><guid>http://www.cnitblog.com/carternzj/archive/2007/07/31/hustnn.html</guid><wfw:comment>http://www.cnitblog.com/carternzj/comments/31061.html</wfw:comment><comments>http://www.cnitblog.com/carternzj/archive/2007/07/31/hustnn.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/carternzj/comments/commentRss/31061.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/carternzj/services/trackbacks/31061.html</trackback:ping><description><![CDATA[<p><span style="FONT-SIZE: 18pt">Linq是Language-Integrated Query的简称，是设置在visual studio 2008上，纳入c#和visual basic语言延伸的强大查询功能．能容易的查询和更新数据，并且能支持多种数据存储．visual studio 2008提供集合使得Linq能查询.net Framework collections,<font style="FONT-SIZE: 18pt" size=3>SQL databases, ADO.NET Datasets, and XML documents.Linq 提供了如下的优点:<br>(1).Linq简化了查询,假如你知道了c#或者visual basic,那么你能轻松的使用Linq;<br>(2)Linq统一任何数据源的数据查询,你能查询xml文档,同样的方式你可以查询sql database,ado.net dataset,in-memory collection和其他分布或者本地的数据源<br>(3)Linq加强了相关数据和面向对象的联系<br>(4)Linq加快的开发和debug<br>下面就对自己学习的过程记录一些自己学习感受,望大家指出不足<br><br>所有的Linq查询有下面基本的3个行为构成:<br>1,包含数据源<br>2,创建查询<br>3,执行查询<br>这里需要特别注意的是,查询的执行严格区别于查询本身,换句话来说就是不会获得任何的数据仅仅通过常见查询,要执行的过程中才会获得.<br>&nbsp;下面就是一段代码,表示了查询操作的3部分<br>//&nbsp; Data source.<br>int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };</font></span></p>
<p><span style="FONT-SIZE: 18pt"><font style="FONT-SIZE: 18pt" size=3>//&nbsp; Query creation.<br>IEnumerable&lt;int&gt; numQuery =<br>&nbsp;&nbsp;&nbsp; from num in numbers<br>&nbsp;&nbsp;&nbsp; where (num % 2) == 0<br>&nbsp;&nbsp;&nbsp; select num;</font></span></p>
<p><span style="FONT-SIZE: 18pt"><font style="FONT-SIZE: 18pt" size=3>//&nbsp; Query execution. <br>foreach (int j in numQuery)<br>{<br>&nbsp;&nbsp;&nbsp; Console.Write("{0,1} ", j);<br>}<br>下面对这3部分做进一步的解释:<br>The Data Source<br>上面例子中的数据源类型是数组,支持IEnumerable接口,是它作为Linq查询的数据源,IEnumerable能被迭代通过foreach,这是Linq查询是如何执行的.支持IEnumerable或者是一个继承于该接口的其他接口,如IQueryable被称为可查询的类型.除了次类可查询的类型,其他的类型例xml document,Linq提供了可查询的类型XElement<br>// Create a data source from an XML document.<br>// using System.Xml.Linq;<br>XElement contacts = XElement.Load(@"c:\myContactList.xml");<br>使用Linq to sql,你首先创建一个对象关系的映射通过O/R Designer,处理与database的通讯,并且Table支持IQueryable<br>// Create a data source from a SQL table.<br>// using System.Data.Linq;<br>DataContext db = new DataContext("c:\\northwind\\northwnd.mdf");<br>Table&lt;Customer&gt; Customers = db.GetTable&lt;Customer&gt;();<br><br>The Query<br>查询是想从数据源获得数据,查询可以指定什么信息应该被选择,被分类等并且什么类型应该被返回,一个查询语句至少应该包含2个语法元素from 和select<br>假如查询要返回一系列的值,查询变量必须是可查询的类型,最重要的是要集注查询变量本身不会采取任何行为,查询也不会返回数据.他仅仅是保存查询的信息,你创建了查询,当你去执行的时候才回获得数据<br><br>Query Execution<br>查询执行分为下面2类:<br>Deferred Execution(推迟执行)<br>当查询创建的时候查询变量本身知识存储查询的命令,真正意义上的查询被推迟到当你通过foreach循环在查询变量上迭代时候.<br>查询中返回一系列结果,查询变量本身不去拥有查询结果,着意味这你可以执行查询随你所需要,数据源没时刻更新,你也可以随时执行查询获得不同的数据.<br>Immediate Execution<br>与前面的不同,即使查询获得单独的一个值而且是立即的,例如Count,<br>Max,Average,First,这些查询立即获得一个单独的值,如下<br>int query = (from num in numbers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where (num % 2) == 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select num).Count();<br>还有强制获得立即查询并且不是获得一个单独的值可以通过调用<br>ToList或者ToArray方法在一个查询变量身上<br>如下<br>List&lt;int&gt; query2 =<br>&nbsp;&nbsp;&nbsp; (from num in numbers<br>&nbsp;&nbsp;&nbsp;&nbsp; where (num % 2) == 0<br>&nbsp;&nbsp;&nbsp;&nbsp; select num).ToList();<br>// or<br>var query3 =<br>&nbsp;&nbsp;&nbsp; (from num in numbers<br>&nbsp;&nbsp;&nbsp;&nbsp; where (num % 2) == 0<br>&nbsp;&nbsp;&nbsp;&nbsp; select num).ToList();<br></font></span></p>
<p>&nbsp;</p>
<img src ="http://www.cnitblog.com/carternzj/aggbug/31061.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/carternzj/" target="_blank">小小牛</a> 2007-07-31 23:26 <a href="http://www.cnitblog.com/carternzj/archive/2007/07/31/hustnn.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>