{"id":548,"date":"2022-06-07T12:25:38","date_gmt":"2022-06-07T04:25:38","guid":{"rendered":"https:\/\/www.mapleriver.cn\/?p=548"},"modified":"2022-06-25T16:05:50","modified_gmt":"2022-06-25T08:05:50","slug":"%e5%ad%a6%e4%b8%80%e4%b8%8blinq%e5%90%a7","status":"publish","type":"post","link":"https:\/\/www.mapleriver.cn\/?p=548","title":{"rendered":"\u5b66\u4e00\u4e0bLINQ\u5427"},"content":{"rendered":"<p>&emsp;\u00a0&emsp;\u5b9e\u5728\u662f\u70e6\u6b7b\u6bcf\u6b21\u8981\u81ea\u5df1\u5199\u5faa\u73af\u627e\u4e1c\u897f\u4e86~<\/p>\n<h6>1.\u9009\u62e9\u67e5\u627e\uff08where\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;where\u67e5\u8be2\u7528\u6765\u67e5\u627e\u6ee1\u8db3\u8981\u6c42\u7684\u5185\u5bb9\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5foreach\u904d\u5386\u8fed\u4ee3\u5668\uff0c\u4e5f\u53ef\u4ee5tolist\u628a\u67e5\u8be2\u7ed3\u679c\u8f6c\u6210\u4e00\u4e2a\u65b0\u7684List\u5b58\u50a8\u3002<\/p>\n<pre><code class=\"language-C#\">\/\/\u67e5\u8be2\u5217\u8868\u4e2dlength&gt;10\u7684\u9879\nvar result = stringList.Where(p =&gt; p.Length &gt; 10).ToList();<\/code><\/pre>\n<h6>2.\u7b80\u5355\u7684\u8ba1\u7b97\uff08min,max,sum,average,count\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u8ba1\u7b97\u4e5f\u53ef\u4ee5\u4f7f\u7528linq\u8fdb\u884c\u8ba1\u7b97(\u7ed3\u679c\u7c7b\u578b\u4e0e\u533f\u540d\u51fd\u6570\u7684\u67e5\u627e\u7c7b\u578b\u76f8\u540c)\uff1a<\/p>\n<pre><code class=\"language-C#\">int min = stringList.Min(p =&gt; p.Length);\/\/\u6700\u5c0f\u503c\nfloat max = floatList.Max();\/\/\u6700\u5927\u503c\nint sum = stringList.Sum(p=&gt;p.Length);\/\/\u5408\nint average = stringList.Average(p=&gt;p.Length);\/\/\u5e73\u5747\u503c\nint count = stringList.Count(p=&gt;p.Length&gt;10);\/\/\u6ee1\u8db3\u6761\u4ef6\u7684\u4e2a\u6570<\/code><\/pre>\n<h6>3.\u6392\u5e8f\uff08orderby\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;\u6392\u5e8f\u4e0e\u7b5b\u9009\u529f\u80fd<\/p>\n<pre><code class=\"language-C#\">\/\/\u6b63\u5e8f\uff0c\u5148\u7b5b\u9009\u51fa\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u540e\u6392\u5e8f\nvar result = stringList.Where(p=&gt;p.Length&gt;10).OrderBy(p=&gt;p.Length);\n\/\/\u5012\u5e8f\nvar result = stringList.Where(p=&gt;p.Length&gt;10).OrderByDescending(p=&gt;p.Length);<\/code><\/pre>\n<h6>4.\u53d6\u7b2c\u4e00\u4e2a\/\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff08First\uff0cLast\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;\u53d6\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u6709\u4e24\u4e2a\u63a5\u53e3\uff0c<code>FirstOrDefault<\/code> \u548c <code>First<\/code> ,\u4e3b\u8981\u533a\u522b\u5c31\u662ffirst\u5728\u53d6\u4e0d\u5230\u7684\u65f6\u5019\u4f1a\u629b\u51fa\u5f02\u5e38\uff0cFirstOrDefault\u5728\u6ca1\u53d6\u5230\u7684\u65f6\u5019\u8fd4\u56denull<\/p>\n<pre><code class=\"language-C#\">string result = stringList.First(p=&gt;p.Length&gt;10);\nstring result = stringList.FirstOrDefault(p=&gt;p.Length&gt;10);\nstring result = stringList.Last(p=&gt;p.Length&gt;10);<\/code><\/pre>\n<h6>5.\u8df3\u8fc7\u6570\u636e\uff0c\u53d6\u6307\u5b9a\u6570\u91cf\u6570\u636e\uff08skip,take\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;\u6bd4\u5982\u5bf9\u6307\u5b9a\u6570\u636e\u8fdb\u884c\u5206\u9875\u67e5\u8be2\uff0c\u6bd4\u5982\u7b2c\u4e00\u9875\u663e\u793a10\u6761\u6570\u636e\uff0c\u73b0\u5728\u8981\u67e5\u7b2c\u4e8c\u9875\u7684\u6570\u636e\uff08\u53d6\u7b2c11\u523020\u6761\u6570\u636e\uff09<\/p>\n<pre><code class=\"language-C#\">var result = stringList.OrderByDescending(p=&gt;p.Length).Skip(10).Take(10).ToList();<\/code><\/pre>\n<h6>6.\u9009\u62e9\u6307\u5b9a\u6570\u636e\uff08select\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;\u5f53\u4f60\u53ea\u60f3\u67e5\u5143\u7d20\u5185\u7684\u6307\u5b9a\u4e00\u79cd\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528select\u8fdb\u884c\u67e5\u8be2\uff0c\u8fd4\u56de\u7684\u5c06\u662f\u4e00\u4e2a\u6307\u5b9a\u7c7b\u578b\u7684\u8fed\u4ee3\u5668<\/p>\n<pre><code class=\"language-C#\">\/\/\u8fd4\u56de\u4e00\u4e2a\u904d\u5386length\u7684\u8fed\u4ee3\u5668\nvar result = stringList.Select(p=&gt;p.Length);<\/code><\/pre>\n<h6>7.\u67e5\u8be2\u7279\u5b9a\u7c7b\u578b\uff08oftype\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;\u4f8b\u5982\u6211\u6709\u4e00\u4e2aobject\u7684list\uff0c\u5176\u4e2d\u5143\u7d20\u5b58\u50a8\u4e86string\u548cint\uff0c\u90a3\u4e48\u6211\u53ef\u4ee5\u4f7f\u7528oftype\u83b7\u53d6\u6211\u60f3\u8981\u7684\u7c7b\u578b<\/p>\n<pre><code class=\"language-C#\">List&lt;object&gt; objList = new List&lt;object&gt;()\n{\n    &quot;test&quot;,\n    1,\n    2.45f,\n    &quot;test2&quot;,\n};\nvar result = objList.OfType&lt;string&gt;();<\/code><\/pre>\n<h6>8.\u53bb\u91cd\uff08Distinct\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;\u8fd9\u4e2a\u53bb\u91cd\u53ea\u80fd\u5bf9\u5143\u7d20\u8fdb\u884c\u53bb\u91cd<\/p>\n<pre><code class=\"language-C#\">List&lt;int&gt; intList = new List&lt;int&gt;(){1,2,1,3,2};\nvar result = intList.Distinct();<\/code><\/pre>\n<p>&emsp;\u00a0&emsp;\u5982\u679c\u60f3\u8981\u6839\u636e\u5143\u7d20\u5185\u7684\u67d0\u4e2a\u5b57\u6bb5\u8fdb\u884c\u53bb\u91cd\uff0c\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e0bIEqualityComparer\u63a5\u53e3\u3002\u5b9e\u73b0\u5176\u4e2d\u7684Equals\u51fd\u6570\u8fdb\u884c\u60f3\u8981\u7684\u53bb\u91cd\u903b\u8f91\u3002<\/p>\n<h6>9.\u5224\u65ad\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\uff08Any,All\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;Any\uff1a\u53ea\u8981\u5b58\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u5219\u4e3atrue.<br \/>\n&emsp;\u00a0&emsp;All\uff1a\u5168\u90fd\u6ee1\u8db3\u6761\u4ef6\u624d\u8fd4\u56detrue.<\/p>\n<pre><code class=\"language-C#\">bool any = stringList.Any(p=&gt;p.Length&gt;10);\nbool all = stringList.All(p=&gt;p.Length&gt;10);<\/code><\/pre>\n<h6>10.\u5c06\u4e00\u4e2a\u96c6\u5408\u4e0e\u53e6\u4e00\u4e2a\u96c6\u5408\u7528\u6307\u5b9a\u5efa\u5408\u5e76\uff0c\u8fd4\u56de\u5408\u5e76\u540e\u7684\u96c6\u5408\uff08Join\uff09<\/h6>\n<p>&emsp;\u00a0&emsp;Join\u4f7f\u7528\u4e86\u4e24\u4e2a\u96c6\u5408\uff0c\u9700\u8981\u4e24\u4e2a\u96c6\u5408\u7684\u67e5\u627e\u5668\uff0c\u4ee5\u53ca\u6700\u540e\u7ed3\u679c\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<pre><code class=\"language-C#\">var result = stringList1.Join(stringList2, list1Ele=&gt;list1Ele.Length, list2Ele=&gt;list2Ele.Length, (p,r)=&gt;new{pList = p, rList = r});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;\u00a0&emsp;\u5b9e\u5728\u662f\u70e6\u6b7b\u6bcf\u6b21\u8981\u81ea\u5df1\u5199\u5faa\u73af\u627e\u4e1c\u897f\u4e86~ 1.\u9009\u62e9\u67e5\u627e\uff08where\uff09 &emsp;\u00a0&#038;e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-548","post","type-post","status-publish","format-standard","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=548"}],"version-history":[{"count":2,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":882,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions\/882"}],"wp:attachment":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}