﻿<?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博客-玄铁剑-文章分类-LinQ</title><link>http://www.cnitblog.com/MartinYao/category/5464.html</link><description>成功的途径：抄，创造，研究，发明...</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 12:56:53 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 12:56:53 GMT</pubDate><ttl>60</ttl><item><title>Linq VS Entity framework</title><link>http://www.cnitblog.com/MartinYao/articles/67203.html</link><dc:creator>玄铁剑</dc:creator><author>玄铁剑</author><pubDate>Tue, 06 Jul 2010 13:27:00 GMT</pubDate><guid>http://www.cnitblog.com/MartinYao/articles/67203.html</guid><wfw:comment>http://www.cnitblog.com/MartinYao/comments/67203.html</wfw:comment><comments>http://www.cnitblog.com/MartinYao/articles/67203.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/MartinYao/comments/commentRss/67203.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/MartinYao/services/trackbacks/67203.html</trackback:ping><description><![CDATA[<p><img title="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" border=0 alt="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" src="https://wr61eq.bay.livefilestore.com/y1mL-FLm34vQ3Kt0XXTC_6jWaq6D16Ae3nyIf7n2WwJdPSz4fpn6uzEiAVFt52t2hmKdTvuseMxkNrqQ7DPJkJPsEUZMdxUSk7C2mefNKPX8k7Ib1IWd7Rfp6CvlbrgpX3pR_cfT2D7lJM/image_thumb[30].png" width=553 height=349> </p>
<div align=center>
<table>
    第二次读写10条数据
    <tbody>
    </tbody>
</table>
</div>
<p><img title="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" border=0 alt="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" src="https://wr61eq.bay.livefilestore.com/y1mOaGxJJmIclM7iP0nrlOkoeKBnl5cxxlEYrYuk-tEGy2PLtpQuKNnF7wxv4CYSYyfHdCuVCw4qeBZkD4z8wk8uW1kW5yy3CCA_cNmieYaCtZkXVILuJCDOi_QA6ehVeemkw8iVV9PWHU/image_thumb[34].png" width=555 height=357> </p>
<div align=center>
<table>
    操作100条数据
    <tbody>
    </tbody>
</table>
</div>
<p><img title="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" border=0 alt="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" src="https://wr61eq.bay.livefilestore.com/y1mZeEY04gWG2ouk8GKlXjWaMS7ubj_KdZDg819afWokBsZamj_r65UhRRCnxK_L32SMrEQYclupVMyVZ5DxojPvYGssqsGMv5D0iORmD64HHHPqqWZ0s_-NSZN3KtANkaV5FEVySDh0z0/image_thumb38.png" width=558 height=347> </p>
<div align=center>
<table>
    操作1000条数据
    <tbody>
    </tbody>
</table>
</div>
<p><img title="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" border=0 alt="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" src="https://wr61eq.bay.livefilestore.com/y1mNU8Y5RO0cdDkVi9QwQI3zFQgWKR7Lvp1XKLN83zOlVDjpiEpEwvmVNPNcG5wbJtNM-NSvDmFX-fFRQ58X2xaGII-xYAsIs2b3sUG1Kr3WZPYIjYtncBfyhzlA43wxi-PW4vlYMxbOec/image_thumb42.png" width=558 height=356> </p>
<div align=center>
<table>
    操作10000条数据
    <tbody>
    </tbody>
</table>
</div>
<p><img title="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" border=0 alt="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" src="https://wr61eq.bay.livefilestore.com/y1m_n5bRSj-pCTH8tRlXN34d16sjMcH5giyrtt6eX7MToLX0ZcTk2vX1Y7egGLykZIPTxoDZDjbItZ1bv5ZCP6aYgHq9pYeER08tf9sDx9Vz3F9w9mh-Eth2SqGcG6mxeB3sTSTHlmzNOQ/image_thumb46.png" width=553 height=349> </p>
<div align=center>
<table>
    操作100000条数据
    <tbody>
    </tbody>
</table>
</div>
<p><img title="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" border=0 alt="ADO.net，Linq to SQL和Entity Framework性能实测分析 — Windows Live - longniezhang - 疯狂计算机技术" src="https://wr61eq.bay.livefilestore.com/y1md5mMqxFYiM4Qpru3CPnn1K47S69f5cBaPKNZBxcwR0r0hE7A5s-BjB5gtFWtaK6bUSYbdMvy3AEo2ooJniXgqVIGkgnBRpChqlDdH0xvnn5FzI5Qt-K_tifxTCC-xBvGY6eSGhzDTzI/image_thumb51.png" width=555 height=355>&nbsp;<br></p>
<p>关于ado.net entity framework 性能比较网上也有很多，这里我只是初步的介绍下ado.net entity framework使用不同的方法查询数据的不同性能</p>
<p>第一部分：重复查询单个实体</p>
<p>第一种：Linq To Entitiess</p>
<p>代码如下：</p>
<p>static　void　Main(string[]　args)<br>　　　　　　　　{<br>　　　　　　　　　　　　DateTime　time1;<br>　　　　　　　　　　　　DateTime　time2;<br>　　　　　　　　　　　　time1　=　DateTime.Now;<br>　　　　　　　　　　　　NorthwindEntities　context　=　new　NorthwindEntities();<br>　　　　　　　　　　　　for　(int　i　=　0;　i　&lt;　1000;　i++)<br>　　　　　　　　　　　　{<br>　　　　　　　　　　　　　　　　var　data　=　(from　c　in　context.Customers　where　c.CustomerID　==　"ALFKI"　select　c).FirstOrDefault();<br>　　　　　　　　　　　　　　　　string　addr　=　data.Address;<br>　　　　　　　　　　　　}<br>　　　　　　　　　　　　time2　=　DateTime.Now;<br>　　　　　　　　　　　　Console.WriteLine((time2-time1).ToString());<br>　　　　　　　　}</p>
<p>查询使用时间为6.2秒左右</p>
<p>第二种：使用Entity SQL</p>
<p>static　void　Main(string[]　args)<br>　　　　　　　　{<br>　　　　　　　　　　　　DateTime　time1;<br>　　　　　　　　　　　　DateTime　time2;<br>　　　　　　　　　　　　time1　=　DateTime.Now;<br>　　　　　　　　　　　　NorthwindEntities　context　=　new　NorthwindEntities();<br>　　　　　　　　　　　　for　(int　i　=　0;　i　&lt;　1000;　i++)<br>　　　　　　　　　　　　{<br>　　　　　　　　　　　　　　　　var　data　=　context.Customers.Where("it.CustomerID=@Id",　new　System.Data.Objects.ObjectParameter("Id",　"ALFKI")).FirstOrDefault();<br>　　　　　　　　　　　　　　　　string　addr　=　data.Address;<br>　　　　　　　　　　　　}<br>　　　　　　　　　　　　time2　=　DateTime.Now;<br>　　　　　　　　　　　　Console.WriteLine((time2-time1).ToString());<br>　　　　　　　　}<br>查询使用时间为6.2秒左右</p>
<p>第三种：使用EntityKey 来查询</p>
<p>　static　void　Main(string[]　args)<br>　　　　　　　　{<br>　　　　　　　　　　　　DateTime　time1;<br>　　　　　　　　　　　　DateTime　time2;<br>　　　　　　　　　　　　time1　=　DateTime.Now;<br>　　　　　　　　　　　　NorthwindEntities　context　=　new　NorthwindEntities();<br>　　　　　　　　　　　　for　(int　i　=　0;　i　&lt;　1000;　i++)<br>　　　　　　　　　　　　{<br>　　　　　　　　　　　　　　　　var　data　=　context.GetObjectByKey(new　System.Data.EntityKey("NorthwindEntities.Customers",　"CustomerID",　"ALFKI"))　as　Customers;<br>　　　　　　　　　　　　　　　　string　addr　=　data.Address;<br>　　　　　　　　　　　　}<br>　　　　　　　　　　　　time2　=　DateTime.Now;<br>　　　　　　　　　　　　Console.WriteLine((time2-time1).ToString());<br>　　　　　　　　}</p>
<p>查询使用时间为1秒，没错是一秒</p>
<p>总结：</p>
<p>前两种方法查询所使用的时间都差不多为6.2秒，时间比较长，但是使用第三种方法查询数据仅仅使用了1秒，为什么会相差那么多，区别在于前两种方法每次查询都要从数据库中查找，1000次查询每次的查询时间都一样，但是第三种方法只有第一次是从数据库中查数据，后面的999次都是在context通过主键直接取数据，速度当然会快很多啦，EF4.0之前的实体当含有外键引用时是自动生成对象引用的，而EF4.0现在多了一个外键ID，这样我们可以很方便的通过外键来查询数据</p>
<p>第二部分：查询不同是实体<br>第一种：Linq To Entitiess</p>
<p>　static　void　Main(string[]　args)<br>　　　　　　　　{<br>　　　　　　　　　　　　DateTime　time1;<br>　　　　　　　　　　　　DateTime　time2;<br>　　　　　　　　　　　　NorthwindEntities　context　=　new　NorthwindEntities();<br>　　　　　　　　　　　　//先将Customers所有的主键查询出来<br>　　　　　　　　　　　　List&lt;string&gt;　keys　=　context.Customers.Select(c　=&gt;　c.CustomerID).Distinct().ToList();<br>　　　　　　　　　　　　//和上面的context以作区别<br>　　　　　　　　　　　　NorthwindEntities　　context_　=　new　NorthwindEntities();<br>　　　　　　　　　　　　time1　=　DateTime.Now;<br>　　　　　　　　　　　　foreach　(var　key　in　keys)<br>　　　　　　　　　　　　{<br>　　　　　　　　　　　　　　　　var　data　=　context_.Customers.Where(c　=&gt;　c.CustomerID　==　key).FirstOrDefault();<br>　　　　　　　　　　　　　　　　string　addr　=　data.Address;<br>　　　　　　　　　　　　}<br>　　　　　　　　　　　　time2　=　DateTime.Now;<br>　　　　　　　　　　　　Console.WriteLine((time2　-　time1).ToString());<br>　　　　　　　　}</p>
<p>查询使用时间0.68秒左右</p>
<p>第二种：使用Entity SQL</p>
<p>static　void　Main(string[]　args)<br>　　　　　　　　{<br>　　　　　　　　　　　　DateTime　time1;<br>　　　　　　　　　　　　DateTime　time2;<br>　　　　　　　　　　　　NorthwindEntities　context　=　new　NorthwindEntities();<br>　　　　　　　　　　　　//先将Customers所有的主键查询出来<br>　　　　　　　　　　　　List&lt;string&gt;　keys　=　context.Customers.Select(c　=&gt;　c.CustomerID).Distinct().ToList();<br>　　　　　　　　　　　　//和上面的context以作区别<br>　　　　　　　　　　　　NorthwindEntities　　context_　=　new　NorthwindEntities();<br>　　　　　　　　　　　　time1　=　DateTime.Now;<br>　　　　　　　　　　　　foreach　(var　key　in　keys)<br>　　　　　　　　　　　　{<br>　　　　　　　　　　　　　　　　var　data　=　context_.Customers.Where("it.CustomerID=@Id",　new　System.Data.Objects.ObjectParameter("Id",　key)).FirstOrDefault();<br>　　　　　　　　　　　　　　　　string　addr　=　data.Address;<br>　　　　　　　　　　　　}<br>　　　　　　　　　　　　time2　=　DateTime.Now;<br>　　　　　　　　　　　　Console.WriteLine((time2　-　time1).ToString());<br>　　　　　　　　}</p>
<p>查询使用时间0.5秒左右</p>
<p>第三种：使用EntityKey 来查询</p>
<p>static　void　Main(string[]　args)<br>　　　　　　　　{<br>　　　　　　　　　　　　DateTime　time1;<br>　　　　　　　　　　　　DateTime　time2;<br>　　　　　　　　　　　　NorthwindEntities　context　=　new　NorthwindEntities();<br>　　　　　　　　　　　　//先将Customers所有的主键查询出来<br>　　　　　　　　　　　　List&lt;string&gt;　keys　=　context.Customers.Select(c　=&gt;　c.CustomerID).Distinct().ToList();<br>　　　　　　　　　　　　//和上面的context以作区别<br>　　　　　　　　　　　　NorthwindEntities　　context_　=　new　NorthwindEntities();<br>　　　　　　　　　　　　time1　=　DateTime.Now;<br>　　　　　　　　　　　　foreach　(var　key　in　keys)<br>　　　　　　　　　　　　{<br>　　　　　　　　　　　　　　　　var　data　=　context_.GetObjectByKey(new　System.Data.EntityKey("NorthwindEntities.Customers",　"CustomerID",　key))　as　Customers;<br>　　　　　　　　　　　　　　　　string　addr　=　data.Address;<br>　　　　　　　　　　　　}<br>　　　　　　　　　　　　time2　=　DateTime.Now;<br>　　　　　　　　　　　　Console.WriteLine((time2　-　time1).ToString());<br>　　　　　　　　}</p>
<p>查询时间为0.18秒左右</p>
<p>总结：</p>
<p>通过比较第三种方法仍然比前两种来的快，查询效率更高</p>
<p>使用EntityKey来查询数据比其他两种方法来的更快，而且不是快一点点，而是相差有好几倍，这里的比较并没有使用存储过程来比较查询，但是从这里我们可以看出，在平常的应用中如果知道实体的主键尽量用主键来查询</p>
<img src ="http://www.cnitblog.com/MartinYao/aggbug/67203.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/MartinYao/" target="_blank">玄铁剑</a> 2010-07-06 21:27 <a href="http://www.cnitblog.com/MartinYao/articles/67203.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Batch Updates and Deletes with LINQ to SQL</title><link>http://www.cnitblog.com/MartinYao/articles/43588.html</link><dc:creator>玄铁剑</dc:creator><author>玄铁剑</author><pubDate>Sat, 10 May 2008 14:19:00 GMT</pubDate><guid>http://www.cnitblog.com/MartinYao/articles/43588.html</guid><wfw:comment>http://www.cnitblog.com/MartinYao/comments/43588.html</wfw:comment><comments>http://www.cnitblog.com/MartinYao/articles/43588.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/MartinYao/comments/commentRss/43588.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/MartinYao/services/trackbacks/43588.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: A couple weeks ago, I read the article, LINQ to SQL Extension: Batch Deletion with Lambda Expression by Jeffrey Zhao.&nbsp; In case you didn't read the article, it discusses the downside of most O/R M...&nbsp;&nbsp;<a href='http://www.cnitblog.com/MartinYao/articles/43588.html'>阅读全文</a><img src ="http://www.cnitblog.com/MartinYao/aggbug/43588.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/MartinYao/" target="_blank">玄铁剑</a> 2008-05-10 22:19 <a href="http://www.cnitblog.com/MartinYao/articles/43588.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Building a ToJSON() Extension Method using .NET 3.5</title><link>http://www.cnitblog.com/MartinYao/articles/38305.html</link><dc:creator>玄铁剑</dc:creator><author>玄铁剑</author><pubDate>Sun, 30 Dec 2007 09:45:00 GMT</pubDate><guid>http://www.cnitblog.com/MartinYao/articles/38305.html</guid><wfw:comment>http://www.cnitblog.com/MartinYao/comments/38305.html</wfw:comment><comments>http://www.cnitblog.com/MartinYao/articles/38305.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/MartinYao/comments/commentRss/38305.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/MartinYao/services/trackbacks/38305.html</trackback:ping><description><![CDATA[<p>Extension methods allow developers to add new methods to the public contract of an existing CLR&nbsp;type, without having to sub-class it or recompile the original type.&nbsp;&nbsp;In doing so they enable a variety of useful scenarios (including LINQ).&nbsp; They also provide a really convenient way to add a dash of "syntactic sugar" into your code.
<p>Over the last few months I've been making a list of cool extension methods that I plan to sit down and implement when I get some free time (not sure when that is... but at least I can still have fun coming up with the ideas!)&nbsp; Two of the&nbsp;scenarios I added to my extension method list were easy methods to automate generating&nbsp;<a href="http://www.json.org/" target=_blank><u><font color=#0000ff>JSON (JavaScript Object Notation)</font></u></a> or XML serialization strings for any .NET object.&nbsp; </p>
<h3><u>Simple Scenario: The ToJSON() extension method</u></h3>
<p>Assume I had a Person object defined like below (note: I'm using the new&nbsp;<a href="http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx" target=_blank><u><font color=#0000ff>automatic properties</font></u></a> feature to implement it):</p>
<p><img src="http://www.scottgu.com/blogposts/tojson/step1.jpg"> </p>
<p>I'd like to then be able to initialize a collection of Person objects and programmatically retrieve a JSON string representation of them by just calling a ToJSON() extension method on it like below:</p>
<p><img src="http://www.scottgu.com/blogposts/tojson/step2.jpg"> </p>
<p>This would work just&nbsp;like the built-in ToString() method on the Object class in .NET today - except that it would generate a JSON-format representation of the collection that I could use for AJAX scenarios on the client:</p>
<p><img src="http://www.scottgu.com/blogposts/tojson/step3.jpg"> </p>
<p>Note: Clicking on the hour-glass in the debugger above allows us to bring up the Text Visualizer in VS to see a clean version of the JSON serialization:</p>
<p><img src="http://www.scottgu.com/blogposts/tojson/step4.jpg"> </p>
<p>This string format could then be used within JavaScript on the client to instantiate an appropriate JavaScript object that represents my collection (note: ASP.NET AJAX has a built-in JavaScript library to support this).</p>
<h3><u>Implementing the ToJSON Extension Method</u></h3>
<p>Implementing a basic ToJSON() extension method is pretty simple.&nbsp; All I needed to-do was use the JavaScriptSerializer class in the System.Web.Script.Serialization namespace, and define two extension methods like below. One of the methods serializes an object graph any levels deep, the other is an overloaded version that allows you to optionally constrain how deep it recurses (for example: ToJSON(2) would serialize only 2 levels deep in the object graph).</p>
<p><img src="http://www.scottgu.com/blogposts/tojson/step5.jpg"> </p>
<p>Note that the ToJSON() extension methods above&nbsp;are defined for type "Object" - which means they can be used with all objects in .NET (not just collections).&nbsp; This means that in addition to calling .ToJSON() on collections like I did above, I could also have called ToJSON() on individual Person objects, as well as any other .NET datatype.</p>
<p>To use the extension method, all I need to-do is add a using statement at the top of my program&nbsp;that references the namespace it was defined within:</p>
<p><img src="http://www.scottgu.com/blogposts/tojson/step6.jpg"> </p>
<p>VS 2008 then takes care of providing intellisense and compile time support for it to all objects:</p>
<p><img src="http://www.scottgu.com/blogposts/tojson/step7.jpg"> </p>
<p><em>Note: In addition to the JavaScriptSerializer class, .NET 3.5 also now includes a </em><a href="http://blogs.msdn.com/kaevans/archive/2007/09/04/use-linq-and-net-3-5-to-convert-rss-to-json.aspx" target=_blank><em><u><font color=#0000ff>new System.Runtime.Serialization.DataContractJsonSerializer class</font></u></em></a><em> that you can use for JSON serialization/deserialization.</em></p>
<h3><u>Summary</u></h3>
<p>Hopefully the above sample provides a simple example of how you can easily encapsulate useful functionality into extension methods.&nbsp; Overtime I expect that we'll start to see some nice utility libraries come out that provide helpful extension methods like above.&nbsp; </p>
<p>I'd be curious to see suggestions for other common scenarios you think should be packaged up into re-usable extension methods (feel free to use the comments of this post to suggest them).&nbsp; We can then figure out how to get a&nbsp;good CodePlex project created that bundles up some of them together into one library to easily use.</p>
<img src ="http://www.cnitblog.com/MartinYao/aggbug/38305.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/MartinYao/" target="_blank">玄铁剑</a> 2007-12-30 17:45 <a href="http://www.cnitblog.com/MartinYao/articles/38305.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>