{"id":124,"date":"2008-05-01T17:19:19","date_gmt":"2008-05-01T17:19:19","guid":{"rendered":""},"modified":"2008-05-01T17:19:19","modified_gmt":"2008-05-01T17:19:19","slug":"124","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=124","title":{"rendered":"LINQ\u4f53\u9a8c(5)\u2014\u2014LINQ to SQL\u8bed\u53e5\u4e4bSel&#101;ct\/Distinct\u548cCount\/Sum"},"content":{"rendered":"<p>\u8fd9\u7bc7\u5f00\u59cb\u6211\u4eec\u7ee7\u7eed\u8bf4LINQ to SQL\u8bed\u53e5\uff0c\u76ee\u7684\u8ba9\u5927\u5bb6\u4ece\u8bed\u53e5\u7684\u89d2\u5ea6\u4e86\u89e3LINQ\uff0cLINQ\u5305\u62ecLINQ to Objects\u3001LINQ to DataSets\u3001LINQ to SQL\u3001LINQ to Entities\u3001LINQ to XML\uff0c\u4f46\u662f\u76f8\u5bf9\u6765\u8bf4LINQ to SQL\u5728\u6211\u4eec\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u6700\u591a\uff0c\u6bd5\u7adf\u6240\u6709\u7684\u6570\u636e\u90fd\u8981\u5728\u6570\u636e\u5e93\u8fd0\u884c\u7740\u5404\u79cd\u64cd\u4f5c\u3002\u6240\u4ee5\u5148\u6765\u5b66\u4e60LINQ to SQL\uff0c\u5176\u5b83\u7684\u90fd\u5dee\u4e0d\u591a\u4e86\uff0c\u90a3\u4e48\u5c31\u4eceSelect\u8bf4\u8d77\u5427\uff0c\u8fd9\u4e2a\u5728\u7f16\u5199\u7a0b\u5e8f\u4e2d\u4e5f\u6700\u4e3a\u5e38\u7528\u3002\u672c\u7bc7\u8be6\u7ec6\u8bf4\u660e\u4e00\u4e0bSelect\u548cCount\/Sum\/Min\/Max\/Avg\u3002<\/p>\n<h1 class=\"hcode\"><font size=\"4\">Select\/Distinct\u64cd\u4f5c\u7b26<\/font><\/h1>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1ao(&cap;_&cap;)o&hellip; \u67e5\u8be2\u5457\u3002<\/p>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u548cSQL\u547d\u4ee4\u4e2d\u7684select\u4f5c\u7528\u76f8\u4f3c\u4f46\u4f4d\u7f6e\u4e0d\u540c\uff0c\u67e5\u8be2\u8868\u8fbe\u5f0f\u4e2d\u7684select\u53ca\u6240\u63a5\u5b50\u53e5\u662f\u653e\u5728\u8868\u8fbe\u5f0f\u6700\u540e\u5e76\u628a\u5b50\u53e5\u4e2d\u7684\u53d8\u91cf\u4e5f\u5c31\u662f\u7ed3\u679c\u8fd4\u56de\u56de\u6765\uff1b\u5ef6\u8fdf\u3002<\/p>\n<p>Select\/Distinct\u64cd\u4f5c\u5305\u62ec9\u79cd\u5f62\u5f0f\uff0c\u5206\u522b\u4e3a\u7b80\u5355\u5f62\u5f0f\u3001\u533f\u540d\u7c7b\u578b\u5f62\u5f0f\u3001\u5e26\u6761\u4ef6\u5f62\u5f0f\u3001\u6307\u5b9a\u7c7b\u578b\u5f62\u5f0f\u3001\u8fc7\u6ee4\u7c7b\u578b\u5f62\u5f0f\u3001shaped\u7c7b\u578b\u5f62\u5f0f\u3001\u5d4c\u5957\u7c7b\u578b\u5f62\u5f0f\u3001LocalMethodCall\u5f62\u5f0f\u3001Distinct\u5f62\u5f0f\u3002<\/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\">select <\/span>c.ContactName;<\/pre>\n<p>\u6ce8\u610f\uff1a\u8fd9\u4e2a\u8bed\u53e5\u53ea\u662f\u4e00\u4e2a\u58f0\u660e\u6216\u8005\u4e00\u4e2a\u63cf\u8ff0\uff0c\u5e76\u6ca1\u6709\u771f\u6b63\u628a\u6570\u636e\u53d6\u51fa\u6765\uff0c\u53ea\u6709\u5f53\u4f60\u9700\u8981\u8be5\u6570\u636e\u7684\u65f6\u5019\uff0c\u5b83\u624d\u4f1a\u6267\u884c\u8fd9\u4e2a\u8bed\u53e5\uff0c\u8fd9\u5c31\u662f\u5ef6\u8fdf\u52a0\u8f7d(deferred loading)\u3002\u5982\u679c\uff0c\u5728\u58f0\u660e\u7684\u65f6\u5019\u5c31\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u662f\u5bf9\u8c61\u7684\u96c6\u5408\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528ToList() \u6216ToArray()\u65b9\u6cd5\u628a\u67e5\u8be2\u7ed3\u679c\u5148\u8fdb\u884c\u4fdd\u5b58\uff0c\u7136\u540e\u518d\u5bf9\u8fd9\u4e2a\u96c6\u5408\u8fdb\u884c\u67e5\u8be2\u3002\u5f53\u7136\u5ef6\u8fdf\u52a0\u8f7d(deferred loading)\u53ef\u4ee5\u50cf\u62fc\u63a5SQL\u8bed\u53e5\u90a3\u6837\u62fc\u63a5\u67e5\u8be2\u8bed\u6cd5\uff0c\u518d\u6267\u884c\u5b83\u3002<\/p>\n<h2>2.\u533f\u540d\u7c7b\u578b\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8bf4\u660e\uff1a\u533f\u540d\u7c7b\u578b\u662fC#3.0\u4e2d\u65b0\u7279\u6027\u3002\u5176\u5b9e\u8d28\u662f\u7f16\u8bd1\u5668\u6839\u636e\u6211\u4eec\u81ea\u5b9a\u4e49\u81ea\u52a8\u4ea7\u751f\u4e00\u4e2a\u533f\u540d\u7684\u7c7b\u6765\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u4e34\u65f6\u53d8\u91cf\u7684\u50a8\u5b58\u3002\u533f\u540d\u7c7b\u578b\u8fd8\u4f9d\u8d56\u4e8e\u53e6\u5916\u4e00\u4e2a\u7279\u6027\uff1a\u652f\u6301\u6839\u636eproperty\u6765\u521b\u5efa\u5bf9\u8c61\u3002\u6bd4\u5982\uff0cvar d = new { Name = &quot;s&quot; };\u7f16\u8bd1\u5668\u81ea\u52a8\u4ea7\u751f\u4e00\u4e2a\u6709property\u53eb\u505aName\u7684\u533f\u540d\u7c7b\uff0c\u7136\u540e\u6309\u8fd9\u4e2a\u7c7b\u578b\u5206\u914d\u5185\u5b58\uff0c\u5e76\u521d\u59cb\u5316\u5bf9\u8c61\u3002\u4f46\u662fvar d = new {&quot;s&quot;};\u662f\u7f16\u8bd1\u4e0d\u901a\u8fc7\u7684\u3002\u56e0\u4e3a\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u533f\u540d\u7c7b\u4e2d\u7684property\u7684\u540d\u5b57\u3002\u4f8b\u5982string c = &quot;d&quot;;var d = new { c}; \u5219\u662f\u53ef\u4ee5\u901a\u8fc7\u7f16\u8bd1\u7684\u3002\u7f16\u8bd1\u5668\u4f1a\u521b\u5efa\u4e00\u4e2a\u53eb\u505a\u533f\u540d\u7c7b\u5e26\u6709\u53ebc\u7684property\u3002<br \/>\u4f8b\u5982\u4e0b\u4f8b\uff1anew{c,ContactName,c.Phone};ContactName\u548cPhone\u90fd\u662f\u5728\u6620\u5c04\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e0e\u8868\u4e2d\u5b57\u6bb5\u76f8\u5bf9\u5e94\u7684property\u3002\u7f16\u8bd1\u5668\u8bfb\u53d6\u6570\u636e\u5e76\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u5c5e\u6027\uff0c\u4e3aContactName\u548cPhone\uff0c\u7136\u540e\u6839\u636e\u6570\u636e\u521d\u59cb\u5316\u5bf9\u8c61\u3002\u53e6\u5916\u7f16\u8bd1\u5668\u8fd8\u53ef\u4ee5\u91cd\u547d\u540dproperty\u7684\u540d\u5b57\u3002<\/p>\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\">select new <\/span>{c.ContactName, c.Phone};<\/pre>\n<p>\u8bed\u53e5\u63cf\u8ff0\uff1a\u67e5\u8be2\u987e\u5ba2\u7684\u8054\u7cfb\u4eba\u548c\u7535\u8bdd\u3002<\/p>\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\">select new\r\n<\/span>{\r\nName = e.FirstName + <span style=\"COLOR: #a31515\">&quot; &quot; <\/span>+ e.LastName,\r\nPhone = e.HomePhone\r\n};<\/pre>\n<p>\u8bed\u53e5\u63cf\u8ff0\uff1a\u67e5\u8be2\u804c\u5458\u7684\u59d3\u540d\u548c\u5bb6\u5ead\u7535\u8bdd<\/p>\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\">select new\r\n<\/span>{\r\np.ProductID,\r\nHalfPrice = p.UnitPrice \/ 2\r\n};<\/pre>\n<h2>3.\u5e26\u6761\u4ef6\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8bf4\u660e\uff1a\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1acase when condition then else\u3002<\/p>\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\">select new\r\n<\/span>{\r\np.ProductName,\r\nAvailability =\r\np.UnitsInStock - p.UnitsOnOrder &lt; 0 ?\r\n<span style=\"COLOR: #a31515\">&quot;Out Of Stock&quot; <\/span>: <span style=\"COLOR: #a31515\">&quot;In Stock&quot;\r\n<\/span>};<\/pre>\n<h2>4.\u6307\u5b9a\u7c7b\u578b\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8bf4\u660e\uff1a\u8be5\u5f62\u5f0f\u8fd4\u56de\u4f60\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5bf9\u8c61\u96c6\u3002<\/p>\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\">select new <\/span><span style=\"COLOR: #2b91af\">Name\r\n<\/span>{\r\nFirstName = e.FirstName,\r\nLastName = e.LastName\r\n};<\/pre>\n<h2>5.\u8fc7\u6ee4\u7c7b\u578b\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8bf4\u660e\uff1a\u7ed3\u5408where\u4f7f\u7528\uff0c\u8d77\u5230\u8fc7\u6ee4\u4f5c\u7528\u3002<\/p>\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.ContactName;<\/pre>\n<h2>6.shaped\u7c7b\u578b\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8bf4\u660e\uff1a\u5176select\u64cd\u4f5c\u4f7f\u7528\u4e86\u533f\u540d\u5bf9\u8c61\uff0c\u800c\u8fd9\u4e2a\u533f\u540d\u5bf9\u8c61\u4e2d\uff0c\u5176\u5c5e\u6027\u4e5f\u662f\u4e2a\u533f\u540d\u5bf9\u8c61\u3002<\/p>\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\">select new <\/span>{\r\nc.CustomerID,\r\nCompanyInfo = <span style=\"COLOR: blue\">new <\/span>{c.CompanyName, c.City, c.Country},\r\nContactInfo = <span style=\"COLOR: blue\">new <\/span>{c.ContactName, c.ContactTitle}\r\n};<\/pre>\n<p>\u8bed\u53e5\u63cf\u8ff0\uff1a\u67e5\u8be2\u987e\u5ba2\u7684ID\u548c\u516c\u53f8\u4fe1\u606f\uff08\u516c\u53f8\u540d\u79f0\uff0c\u57ce\u5e02\uff0c\u56fd\u5bb6\uff09\u4ee5\u53ca\u8054\u7cfb\u4fe1\u606f\uff08\u8054\u7cfb\u4eba\u548c\u804c\u4f4d\uff09\u3002<\/p>\n<h2>7.\u5d4c\u5957\u7c7b\u578b\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8bf4\u660e\uff1a\u8fd4\u56de\u7684\u5bf9\u8c61\u96c6\u4e2d\u7684\u6bcf\u4e2a\u5bf9\u8c61DiscountedProducts\u5c5e\u6027\u4e2d\uff0c\u53c8\u5305\u542b\u4e00\u4e2a\u96c6\u5408\u3002\u4e5f\u5c31\u662f\u6bcf\u4e2a\u5bf9\u8c61\u4e5f\u662f\u4e00\u4e2a\u96c6\u5408\u7c7b\u3002<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q =\r\n<span style=\"COLOR: blue\">from <\/span>o <span style=\"COLOR: blue\">in <\/span>db.Orders\r\n<span style=\"COLOR: blue\">select new <\/span>{\r\no.OrderID,\r\nDiscountedProducts =\r\n<span style=\"COLOR: blue\">from <\/span>od <span style=\"COLOR: blue\">in <\/span>o.OrderDetails\r\n<span style=\"COLOR: blue\">where <\/span>od.Discount &gt; 0.0\r\n<span style=\"COLOR: blue\">select <\/span>od,\r\nFreeShippingDiscount = o.Freight\r\n};<\/pre>\n<h2>8.LocalMethodCall\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8fd9\u4e2a\u4f8b\u5b50InternationalPhone\u8c03\u7528\u672c\u5730\u65b9\u6cd5PhoneNumberConverter<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = <span style=\"COLOR: blue\">from <\/span>c <span style=\"COLOR: blue\">in <\/span>db.Customers\r\n<span style=\"COLOR: blue\">where <\/span>c.Country == <span style=\"COLOR: #a31515\">&quot;UK&quot; <\/span>|| c.Country == <span style=\"COLOR: #a31515\">&quot;USA&quot;\r\n<\/span><span style=\"COLOR: blue\">select new\r\n<\/span>{\r\nc.CustomerID,\r\nc.CompanyName,\r\nPhone = c.Phone,\r\nInternationalPhone =\r\nPhoneNumberConverter(c.Country, c.Phone)\r\n};<\/pre>\n<p>PhoneNumberConverter\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">public string <\/span>PhoneNumberConverter(<span style=\"COLOR: blue\">string <\/span>Country, <span style=\"COLOR: blue\">string <\/span>Phone)\r\n{\r\nPhone = Phone.Replace(<span style=\"COLOR: #a31515\">&quot; &quot;<\/span>, <span style=\"COLOR: #a31515\">&quot;&quot;<\/span>).Replace(<span style=\"COLOR: #a31515\">&quot;)&quot;<\/span>, <span style=\"COLOR: #a31515\">&quot;)-&quot;<\/span>);\r\n<span style=\"COLOR: blue\">switch <\/span>(Country)\r\n{\r\n<span style=\"COLOR: blue\">case <\/span><span style=\"COLOR: #a31515\">&quot;USA&quot;<\/span>:\r\n<span style=\"COLOR: blue\">return <\/span><span style=\"COLOR: #a31515\">&quot;1-&quot; <\/span>+ Phone;\r\n<span style=\"COLOR: blue\">case <\/span><span style=\"COLOR: #a31515\">&quot;UK&quot;<\/span>:\r\n<span style=\"COLOR: blue\">return <\/span><span style=\"COLOR: #a31515\">&quot;44-&quot; <\/span>+ Phone;\r\n<span style=\"COLOR: blue\">default<\/span>:\r\n<span style=\"COLOR: blue\">return <\/span>Phone;\r\n}\r\n}<\/pre>\n<p>\u4e0b\u9762\u4e5f\u662f\u4f7f\u7528\u4e86\u8fd9\u4e2a\u65b9\u6cd5<\/p>\n<pre class=\"code\"><span style=\"COLOR: #2b91af\">XDocument <\/span>doc = <span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">XDocument<\/span>(\r\n<span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">XElement<\/span>(<span style=\"COLOR: #a31515\">&quot;Customers&quot;<\/span>, <span style=\"COLOR: blue\">from <\/span>c <span style=\"COLOR: blue\">in <\/span>db.Customers\r\n<span style=\"COLOR: blue\">where <\/span>c.Country == <span style=\"COLOR: #a31515\">&quot;UK&quot; <\/span>|| c.Country == <span style=\"COLOR: #a31515\">&quot;USA&quot;\r\n<\/span><span style=\"COLOR: blue\">select <\/span>(<span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">XElement<\/span>(<span style=\"COLOR: #a31515\">&quot;Customer&quot;<\/span>,\r\n<span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">XAttribute<\/span>(<span style=\"COLOR: #a31515\">&quot;CustomerID&quot;<\/span>, c.CustomerID),\r\n<span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">XAttribute<\/span>(<span style=\"COLOR: #a31515\">&quot;CompanyName&quot;<\/span>, c.CompanyName),\r\n<span style=\"COLOR: blue\">new <\/span><span style=\"COLOR: #2b91af\">XAttribute<\/span>(<span style=\"COLOR: #a31515\">&quot;InterationalPhone&quot;<\/span>,\r\nPhoneNumberConverter(c.Country, c.Phone))\r\n))));<\/pre>\n<h2>9.Distinct\u5f62\u5f0f\uff1a<\/h2>\n<p>\u8bf4\u660e\uff1a\u7b5b\u9009\u5b57\u6bb5\u4e2d\u4e0d\u76f8\u540c\u7684\u503c\u3002\u7528\u4e8e\u67e5\u8be2\u4e0d\u91cd\u590d\u7684\u7ed3\u679c\u96c6\u3002\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1aSelect DISTINCT [City] FROM [Customers]<\/p>\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\">select <\/span>c.City )\r\n.Distinct();<\/pre>\n<p>\u8bed\u53e5\u63cf\u8ff0\uff1a\u67e5\u8be2\u987e\u5ba2\u8986\u76d6\u7684\u56fd\u5bb6\u3002<\/p>\n<h1 class=\"hcode\"><font size=\"4\">Count\/Sum\/Min\/Max\/Avg\u64cd\u4f5c\u7b26<\/font><\/h1>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u7edf\u8ba1\u6570\u636e\u5427\uff0c\u6bd4\u5982\u7edf\u8ba1\u4e00\u4e9b\u6570\u636e\u7684\u4e2a\u6570\uff0c\u6c42\u548c\uff0c\u6700\u5c0f\u503c\uff0c\u6700\u5927\u503c\uff0c\u5e73\u5747\u6570\u3002<\/p>\n<h2>Count<\/h2>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8fd4\u56deINT\u7c7b\u578b\uff1b\u4e0d\u5ef6\u8fdf\u3002\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1aSelect COUNT(*) FROM <\/p>\n<h3>1.\u7b80\u5355\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Customers.Count();<\/pre>\n<h3>2.\u5e26\u6761\u4ef6\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Products.Count(p =&gt; !p.Discontinued);<\/pre>\n<h2>LongCount<\/h2>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8fd4\u56deLONG\u7c7b\u578b\uff1b\u4e0d\u5ef6\u8fdf\u3002\u5bf9\u4e8e\u5143\u7d20\u4e2a\u6570\u8f83\u591a\u7684\u96c6\u5408\u53ef\u89c6\u60c5\u51b5\u53ef\u4ee5\u9009\u7528LongCount\u6765\u7edf\u8ba1\u5143\u7d20\u4e2a\u6570\uff0c\u5b83\u8fd4\u56delong\u7c7b\u578b\uff0c\u6bd4\u8f83\u7cbe\u786e\u3002\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1aSelect COUNT_BIG(*) FROM <\/p>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Customers.LongCount();<\/pre>\n<h2>Sum<\/h2>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u8fd4\u56de\u96c6\u5408\u4e2d\u6570\u503c\u7c7b\u578b\u5143\u7d20\u4e4b\u548c\uff0c\u96c6\u5408\u5e94\u4e3aINT\u7c7b\u578b\u96c6\u5408\uff1b\u4e0d\u5ef6\u8fdf\u3002\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1aSelect SUM(&hellip;) FROM <\/p>\n<h3>1.\u7b80\u5355\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Orders.Select(o =&gt; o.Freight).Sum();<\/pre>\n<h3>2.\u6620\u5c04\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Products.Sum(p =&gt; p.UnitsOnOrder);<\/pre>\n<h2>Min<\/h2>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u8fd4\u56de\u96c6\u5408\u4e2d\u5143\u7d20\u7684\u6700\u5c0f\u503c\uff1b\u4e0d\u5ef6\u8fdf\u3002\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1aSelect MIN(&hellip;) FROM <\/p>\n<h3>1.\u7b80\u5355\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Products.Select(p =&gt; p.UnitPrice).Min();<\/pre>\n<h3>2.\u6620\u5c04\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Orders.Min(o =&gt; o.Freight);<\/pre>\n<h3>3.\u539f\u7406\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>categories =\r\n<span style=\"COLOR: blue\">from <\/span>p <span style=\"COLOR: blue\">in <\/span>db.Products\r\n<span style=\"COLOR: blue\">group <\/span>p <span style=\"COLOR: blue\">by <\/span>p.CategoryID <span style=\"COLOR: blue\">into <\/span>g\r\n<span style=\"COLOR: blue\">select new <\/span>{\r\nCategoryID = g.Key,\r\nCheapestProducts =\r\n<span style=\"COLOR: blue\">from <\/span>p2 <span style=\"COLOR: blue\">in <\/span>g\r\n<span style=\"COLOR: blue\">where <\/span>p2.UnitPrice == g.Min(p3 =&gt; p3.UnitPrice)\r\n<span style=\"COLOR: blue\">select <\/span>p2\r\n};<\/pre>\n<h2>Max<\/h2>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u8fd4\u56de\u96c6\u5408\u4e2d\u5143\u7d20\u7684\u6700\u5927\u503c\uff1b\u4e0d\u5ef6\u8fdf\u3002\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1aSelect MAX(&hellip;) FROM<\/p>\n<h3>1.\u7b80\u5355\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Employees.Select(e =&gt; e.HireDate).Max();<\/pre>\n<h3>2.\u6620\u5c04\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Products.Max(p =&gt; p.UnitsInStock);<\/pre>\n<h3>3.\u539f\u7406\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>categories =\r\n<span style=\"COLOR: blue\">from <\/span>p <span style=\"COLOR: blue\">in <\/span>db.Products\r\n<span style=\"COLOR: blue\">group <\/span>p <span style=\"COLOR: blue\">by <\/span>p.CategoryID <span style=\"COLOR: blue\">into <\/span>g\r\n<span style=\"COLOR: blue\">select new <\/span>{\r\ng.Key,\r\nMostExpensiveProducts =\r\n<span style=\"COLOR: blue\">from <\/span>p2 <span style=\"COLOR: blue\">in <\/span>g\r\n<span style=\"COLOR: blue\">where <\/span>p2.UnitPrice == g.Max(p3 =&gt; p3.UnitPrice)\r\n<span style=\"COLOR: blue\">select <\/span>p2\r\n};<\/pre>\n<h2>Average<\/h2>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u6570\u503c\u7c7b\u578b\u5143\u7d20\u7684\u5e73\u5747\u503c\u3002\u96c6\u5408\u5e94\u4e3a\u6570\u5b57\u7c7b\u578b\u96c6\u5408\uff0c\u5176\u8fd4\u56de\u503c\u7c7b\u578b\u4e3adouble\uff1b\u4e0d\u5ef6\u8fdf\u3002\u751f\u6210SQL\u8bed\u53e5\u4e3a\uff1aSelect AVG(&hellip;) FROM<\/p>\n<h3>1.\u7b80\u5355\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Orders.Select(o =&gt; o.Freight).Average();<\/pre>\n<h3>2.\u6620\u5c04\u5f62\u5f0f\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>q = db.Products.Average(p =&gt; p.UnitPrice);<\/pre>\n<h3>3.\u539f\u7406\uff1a<\/h3>\n<pre class=\"code\"><span style=\"COLOR: blue\">var <\/span>categories =\r\n<span style=\"COLOR: blue\">from <\/span>p <span style=\"COLOR: blue\">in <\/span>db.Products\r\n<span style=\"COLOR: blue\">group <\/span>p <span style=\"COLOR: blue\">by <\/span>p.CategoryID <span style=\"COLOR: blue\">into <\/span>g\r\n<span style=\"COLOR: blue\">select new <\/span>{\r\ng.Key,\r\nExpensiveProducts =\r\n<span style=\"COLOR: blue\">from <\/span>p2 <span style=\"COLOR: blue\">in <\/span>g\r\n<span style=\"COLOR: blue\">where <\/span>p2.UnitPrice &gt; g.Average(p3 =&gt; p3.UnitPrice)\r\n<span style=\"COLOR: blue\">select <\/span>p2\r\n};<\/pre>\n<h2>Aggregate<\/h2>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a\u6839\u636e\u8f93\u5165\u7684\u8868\u8fbe\u5f0f\u83b7\u53d6\u805a\u5408\u503c\uff1b\u4e0d\u5ef6\u8fdf\u3002\u5373\u662f\u8bf4\uff1a\u7528\u4e00\u4e2a\u79cd\u5b50\u503c\u4e0e\u5f53\u524d\u5143\u7d20\u901a\u8fc7\u6307\u5b9a\u7684\u51fd\u6570\u6765\u8fdb\u884c\u5bf9\u6bd4\u6765\u904d\u5386\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u4fdd\u7559\u4e0b\u6765\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u79cd\u5b50\u503c\u7684\u8bdd\uff0c\u79cd\u5b50\u503c\u9ed8\u8ba4\u4e3a\u96c6\u5408\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u4e0b\u9762\u7528\u4e00\u4e2a\u8868\u683c\u603b\u7ed3\u4e00\u4e0b\u8fd9\u7bc7\u8bf4\u7684LINQ\u8bed\u53e5<\/p>\n<table style=\"WIDTH: 80%\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" border=\"1\">\n<tbody>\n<tr>\n<td>Where<\/td>\n<td>\u8fc7\u6ee4\uff1b\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Select<\/td>\n<td>\u9009\u62e9\uff1b\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Distinct<\/td>\n<td>\u67e5\u8be2\u4e0d\u91cd\u590d\u7684\u7ed3\u679c\u96c6\uff1b\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Count<\/td>\n<td>\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8fd4\u56deINT\u7c7b\u578b\uff1b\u4e0d\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>LongCount<\/td>\n<td>\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8fd4\u56deLONG\u7c7b\u578b\uff1b\u4e0d\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Sum<\/td>\n<td>\u8fd4\u56de\u96c6\u5408\u4e2d\u6570\u503c\u7c7b\u578b\u5143\u7d20\u4e4b\u548c\uff0c\u96c6\u5408\u5e94\u4e3aINT\u7c7b\u578b\u96c6\u5408\uff1b\u4e0d\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Min<\/td>\n<td>\u8fd4\u56de\u96c6\u5408\u4e2d\u5143\u7d20\u7684\u6700\u5c0f\u503c\uff1b\u4e0d\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Max<\/td>\n<td>\u8fd4\u56de\u96c6\u5408\u4e2d\u5143\u7d20\u7684\u6700\u5927\u503c\uff1b\u4e0d\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Average<\/td>\n<td>\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u6570\u503c\u7c7b\u578b\u5143\u7d20\u7684\u5e73\u5747\u503c\u3002\u96c6\u5408\u5e94\u4e3a\u6570\u5b57\u7c7b\u578b\u96c6\u5408\uff0c\u5176\u8fd4\u56de\u503c\u7c7b\u578b\u4e3adouble\uff1b\u4e0d\u5ef6\u8fdf<\/td>\n<\/tr>\n<tr>\n<td>Aggregate <\/td>\n<td>\u6839\u636e\u8f93\u5165\u7684\u8868\u8fbe\u5f0f\u83b7\u53d6\u805a\u5408\u503c\uff1b\u4e0d\u5ef6\u8fdf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u5f00\u59cb\u6211\u4eec\u7ee7\u7eed\u8bf4LINQ to SQL\u8bed\u53e5\uff0c\u76ee\u7684\u8ba9\u5927\u5bb6\u4ece\u8bed\u53e5\u7684\u89d2\u5ea6&#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-124","post","type-post","status-publish","format-standard","hentry","category-NET"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/124","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=124"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}