{"id":123,"date":"2008-05-01T17:14:48","date_gmt":"2008-05-01T17:14:48","guid":{"rendered":""},"modified":"2008-05-01T17:14:48","modified_gmt":"2008-05-01T17:14:48","slug":"123","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=123","title":{"rendered":"LINQ\u4f53\u9a8c(4)\u2014\u2014LINQ\u7b80\u4ecb\u548cLINQ to SQL\u8bed\u53e5\u4e4bWh&#101;re"},"content":{"rendered":"<h2 class=\"hcode\">\u67e5\u8be2\u8868\u8fbe\u5f0f(LINQ)\u7b80\u4ecb<\/h2>\n<p>\u5728\u4e0a\u4e24\u7bc7\u6211\u4ecb\u7ecd\u4e86C#3.0\u65b0\u8bed\u7279\u6027\u548c\u6539\u8fdb\uff0c\u8fd9\u4e9b\u65b0\u7279\u6027\u5728\u6211\u4eec\u7f16\u5199\u7a0b\u5e8f\u65f6\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u975e\u5e38\u5927\u7684\u5e2e\u52a9\u3002\u4ece\u8fd9\u7bc7\u5f00\u59cb\uff0c\u6211\u4eec\u5f00\u59cb\u4e00\u8d77\u6765\u63a2\u8ba8LINQ\u3002<\/p>\n<p>LINQ\u662fLanguage Integrated Query\u7684\u7b80\u79f0\uff0c\u5b83\u662f\u96c6\u6210\u5728.NET\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u4e00\u79cd\u7279\u6027\u3002\u5df2\u6210\u4e3a\u7f16\u7a0b\u8bed\u8a00\u7684\u4e00\u4e2a\u7ec4\u6210\u90e8\u5206\uff0c\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\u53ef\u4ee5\u5f97\u5230\u5f88\u597d\u7684\u7f16\u8bd1\u65f6\u8bed\u6cd5\u68c0\u67e5\uff0c\u4e30\u5bcc\u7684\u5143\u6570\u636e\uff0c\u667a\u80fd\u611f\u77e5\u3001\u9759\u6001\u7c7b\u578b\u7b49\u5f3a\u7c7b\u578b\u8bed\u8a00\u7684\u597d\u5904\u3002\u5e76\u4e14\u5b83\u540c\u65f6\u8fd8\u4f7f\u5f97\u67e5\u8be2\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5185\u5b58\u4e2d\u7684\u4fe1\u606f\u8fdb\u884c\u67e5\u8be2\u800c\u4e0d\u4ec5\u4ec5\u53ea\u662f\u5916\u90e8\u6570\u636e\u6e90\u3002<\/p>\n<p>LINQ\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6807\u51c6\u67e5\u8be2\u64cd\u4f5c\u7b26\u7528\u4e8e\u5728\u6240\u6709\u57fa\u4e8e.NET\u5e73\u53f0\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\u66f4\u52a0\u76f4\u63a5\u5730\u58f0\u660e\u8de8\u8d8a\u3001\u8fc7\u6ee4\u548c\u6295\u5c04\u64cd\u4f5c\u7684\u7edf\u4e00\u65b9\u5f0f\uff0c\u6807\u51c6\u67e5\u8be2\u64cd\u4f5c\u7b26\u5141\u8bb8\u67e5\u8be2\u4f5c\u7528\u4e8e\u6240\u6709\u57fa\u4e8eIEnumerable&lt;T&gt;\u63a5\u53e3\u7684\u6e90\uff0c\u5e76\u4e14\u5b83\u8fd8\u5141\u8bb8\u9002\u5408\u4e8e\u76ee\u6807\u57df\u6216\u6280\u672f\u7684\u7b2c\u4e09\u65b9\u7279\u5b9a\u57df\u64cd\u4f5c\u7b26\u6765\u6269\u5927\u6807\u51c6\u67e5\u8be2\u64cd\u4f5c\u7b26\u96c6\uff0c\u66f4\u91cd\u8981\u7684\u662f\uff0c\u7b2c\u4e09\u65b9\u64cd\u4f5c\u7b26\u53ef\u4ee5\u7528\u5b83\u4eec\u81ea\u5df1\u7684\u63d0\u4f9b\u9644\u52a0\u670d\u52a1\u7684\u5b9e\u73b0\u6765\u81ea\u7531\u5730\u66ff\u6362\u6807\u51c6\u67e5\u8be2\u64cd\u4f5c\u7b26\uff0c\u6839\u636eLINQ\u6a21\u5f0f\u7684\u4e60\u4fd7\uff0c\u8fd9\u4e9b\u67e5\u8be2\u559c\u6b22\u91c7\u7528\u4e0e\u6807\u51c6\u67e5\u8be2\u64cd\u4f5c\u7b26\u76f8\u540c\u7684\u8bed\u8a00\u96c6\u6210\u548c\u5de5\u5177\u652f\u6301\u3002<\/p>\n<p>\u6211\u4eec\u6765\u603b\u4f53\u770b\u770bLINQ\u67b6\u6784<img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/72008519171057.gif\" \/><\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ10.gif\" target=\"_blank\"><\/a><\/p>\n<p>\u5728.NET3.5\u4e0b\uff0c\u5fae\u8f6f\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e9b\u547d\u540d\u7a7a\u95f4<\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ11.gif\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" height=\"439\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/m2008519171124.gif\" width=\"550\" border=\"0\" \/><\/a><\/p>\n<p>LINQ\u5305\u62ec\u4e94\u4e2a\u90e8\u5206\uff1aLINQ to Objects\u3001LINQ to DataSets\u3001LINQ to SQL\u3001LINQ to Entities\u3001LINQ to XML\u3002<\/p>\n<p>LINQ to SQL\u5168\u79f0\u57fa\u4e8e\u5173\u7cfb\u6570\u636e\u7684.NET\u8bed\u8a00\u96c6\u6210\u67e5\u8be2\uff0c\u7528\u4e8e\u4ee5\u5bf9\u8c61\u5f62\u5f0f\u7ba1\u7406\u5173\u7cfb\u6570\u636e\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u67e5\u8be2\u529f\u80fd\u3002\u5176\u5efa\u7acb\u4e8e\u516c\u5171\u8bed\u8a00\u7c7b\u578b\u7cfb\u7edf\u4e2d\u7684\u57fa\u4e8eSQL\u7684\u6a21\u5f0f\u5b9a\u4e49\u7684\u96c6\u6210\u4e4b\u4e0a\uff0c\u5f53\u4fdd\u6301\u5173\u7cfb\u578b\u6a21\u578b\u8868\u8fbe\u80fd\u529b\u548c\u5bf9\u5e95\u5c42\u5b58\u50a8\u7684\u76f4\u63a5\u67e5\u8be2\u8bc4\u6d4b\u7684\u6027\u80fd\u65f6\uff0c\u8fd9\u4e2a\u96c6\u6210\u5728\u5173\u7cfb\u578b\u6570\u636e\u4e4b\u4e0a\u63d0\u4f9b\u5f3a\u7c7b\u578b\u3002<\/p>\n<p>LINQ to XML\u5728System.Xml.LINQ\u547d\u540d\u7a7a\u95f4\u4e0b\u5b9e\u73b0\u5bf9XML\u7684\u64cd\u4f5c\u3002\u91c7\u7528\u9ad8\u6548\u3001\u6613\u7528\u3001\u5185\u5b58\u4e2d\u7684XML\u5de5\u5177\u5728\u5bbf\u4e3b\u7f16\u7a0b\u8bed\u8a00\u4e2d\u63d0\u4f9bXPath\/XQuery\u529f\u80fd\u7b49\u3002<\/p>\n<p>\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u6211\u4eec\u8fd8\u662f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\u8bf4\u660e\u4e00\u4e0bLINQ\u5e26\u6765\u7684\u4f53\u9a8c\u3002<\/p>\n<p>\u7b2c\u4e00\u6b65\uff1a\u5efa\u7acbdbml\uff08Database Mark Language\u3002\u6570\u636e\u5e93\u63cf\u8ff0\u8bed\u8a00\uff0c\u662f\u4e00\u79cdxml\u683c\u5f0f\u7684\u6587\u6863\uff0c\u7528\u6765\u63cf\u8ff0\u6570\u636e\u5e93\uff09\u6587\u4ef6\uff0c\u4ee5Northwind\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u4e0a\u8ff0Customers\u7c7b\u88ab\u6620\u5c04\u6210\u4e00\u4e2a\u8868\uff0c\u5bf9\u5e94\u6570\u636e\u5e93\u4e2d\u7684 Customers\u8868 <\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ12.gif\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" height=\"400\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/52008519171320.gif\" width=\"551\" border=\"0\" \/><\/a><\/p>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa\u4e00\u4e2aASP.NET\u9875\u9762\uff0c\u5728\u9875\u9762\u4e0a\u52a0\u5165\u4e00\u4e2aGridView\u63a7\u4ef6 <img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/z2008519171353.gif\" \/><\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ13.gif\" target=\"_blank\"><\/a><\/p>\n<p>\u7b2c\u4e09\u6b65\uff1a\u7f16\u5199\u4ee3\u7801\u8fdb\u884c\u6570\u636e\u7ed1\u5b9a <br \/><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/w2008519171413.gif\" \/><\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ14.gif\" target=\"_blank\"><\/a><\/p>\n<p>\u7b2c\u56db\u6b65\uff1a\u8fd0\u884c\u663e\u793a\u7ed3\u679c\u3002<br \/><img decoding=\"async\" alt=\"\" src=\"http:\/\/www.poloo.org\/attachments\/month_0805\/z2008519171430.gif\" \/><\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ15.gif\" target=\"_blank\"><\/a><\/p>\n<p>\u597d\u4e86\uff0c\u5c31\u8bf4\u8fd9\u4e48\u591a\u5427\uff0c\u5927\u5bb6\u5e94\u8be5\u5bf9LINQ\u6709\u4e86\u603b\u4f53\u7684\u4e86\u89e3\u3002\u6700\u540e\u6211\u5bf9LINQ\u8fd8\u6709\u4e00\u70b9\u7591\u60d1\uff0c\u5728\u6b64\u63d0\u51fa\uff0c\u8bf7\u719f\u6089\u7684\u670b\u53cb\u6765\u63a2\u8ba8\uff1a<\/p>\n<ol>\n<li>LINQ\u662f\u5728ADO.NET\u4e4b\u4e0a\u7684\uff0c\u90a3\u4e48\u5728\u5c06\u6765\u5b83\u4f1a\u4ee3\u66ffADO.NET\u5417\uff1f <\/li>\n<li>\u5728\u5927\u578b\u9879\u76ee\u4e2d\u4f7f\u7528LINQ\uff0c\u5b83\u7684\u6548\u7387\u5982\u4f55\u5462\uff1f <\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5f00\u59cb\u4eceLINQ to SQL\u8bed\u53e5\u5165\u624b\uff0c\u6765\u5168\u9762\u4e86\u89e3\u4e00\u4e0bLINQ\uff0c\u5c31\u4ece\u6700\u7b80\u5355\u7684Where\u8bf4\u8d77\u5427\uff0c\u8fd9\u4e2a\u5728\u7f16\u5199\u7a0b\u5e8f\u4e2d\u4e5f\u6700\u4e3a\u5e38\u7528\u3002<\/p>\n<h2 class=\"hcode\">Where\u64cd\u4f5c<\/h2>\n<p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong>\u5b9e\u73b0\u8fc7\u6ee4\uff0c\u67e5\u8be2\u7b49\u529f\u80fd\u3002<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong>\u4e0eSQL\u547d\u4ee4\u4e2d\u7684Where\u4f5c\u7528\u76f8\u4f3c\uff0c\u90fd\u662f\u8d77\u5230\u8303\u56f4\u9650\u5b9a\u4e5f\u5c31\u662f\u8fc7\u6ee4\u4f5c\u7528\u7684\uff0c\u800c\u5224\u65ad\u6761\u4ef6\u5c31\u662f\u5b83\u540e\u9762\u6240\u63a5\u7684\u5b50\u53e5\u3002<\/p>\n<p>Where\u64cd\u4f5c\u5305\u62ec3\u79cd\u5f62\u5f0f\uff0c\u5206\u522b\u4e3a\u7b80\u5355\u5f62\u5f0f\u3001\u5173\u7cfb\u6761\u4ef6\u5f62\u5f0f\u3001First()\u5f62\u5f0f\u3002\u4e0b\u9762\u5206\u522b\u7528\u5b9e\u4f8b\u4e3e\u4f8b\u4e0b\uff1a<\/p>\n<h2>1.\u7b80\u5355\u5f62\u5f0f\uff1a<\/h2>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q =\r\n<span style=\"COLOR: blue\">from <\/span>c <span style=\"COLOR: blue\">in <\/span>db.Customers\r\n<span style=\"COLOR: blue\">where <\/span>c.City == <span style=\"COLOR: #a31515\">&quot;London&quot;\r\n<\/span><span style=\"COLOR: blue\">select <\/span>c;<\/pre>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q =\r\n<span style=\"COLOR: blue\">from <\/span>e <span style=\"COLOR: blue\">in <\/span>db.Employees\r\n<span style=\"COLOR: blue\">where <\/span>e.HireDate &gt;= <span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">DateTime<\/span>(1994, 1, 1)\r\n<span style=\"COLOR: blue\">select <\/span>e;<\/pre>\n<h2>2.\u5173\u7cfb\u6761\u4ef6\u5f62\u5f0f\uff1a<\/h2>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q =\r\n<span style=\"COLOR: blue\">from <\/span>p <span style=\"COLOR: blue\">in <\/span>db.Products\r\n<span style=\"COLOR: blue\">where <\/span>p.UnitsInStock &lt;= p.ReorderLevel &amp;&amp; !p.Discontinued\r\n<span style=\"COLOR: blue\">select <\/span>p;<\/pre>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q =\r\n<span style=\"COLOR: blue\">from <\/span>p <span style=\"COLOR: blue\">in <\/span>db.Products\r\n<span style=\"COLOR: blue\">where <\/span>p.UnitPrice &gt; 10m || p.Discontinued\r\n<span style=\"COLOR: blue\">select <\/span>p;<\/pre>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q =\r\ndb.Products.Where(p=&gt;p.UnitPrice &gt; 10m).Where(p=&gt;p.Discontinued);<\/pre>\n<h2>3.First()\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u5176\u5b9e\u8d28\u5c31\u662f\u5728SQL\u8bed\u53e5\u4e2d\u52a0TOP (1)<\/p>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">Shipper <\/span>shipper = db.Shippers.First();<\/pre>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">Customer <\/span>cust = db.Customers.First(c =&gt; c.CustomerID == <span style=\"COLOR: #a31515\">&quot;BONAP&quot;<\/span>);<\/pre>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">Order <\/span>ord = db.Orders.First(o =&gt; o.Freight &gt; 10.00M);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u8be2\u8868\u8fbe\u5f0f(LINQ)\u7b80\u4ecb \u5728\u4e0a\u4e24\u7bc7\u6211\u4ecb\u7ecd\u4e86C#3.0\u65b0\u8bed\u7279\u6027\u548c\u6539\u8fdb&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-NET"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}