{"id":29,"date":"2023-10-18T16:07:11","date_gmt":"2023-10-18T16:07:11","guid":{"rendered":"http:\/\/kenneth.mobi\/?p=29"},"modified":"2023-10-18T16:39:44","modified_gmt":"2023-10-18T16:39:44","slug":"linq-%e5%9f%ba%e6%9c%ac%e6%95%99%e5%ad%b8","status":"publish","type":"post","link":"https:\/\/www.kenneth.mobi\/?p=29","title":{"rendered":"LINQ \u57fa\u672c\u6559\u5b78"},"content":{"rendered":"<p>test <\/p>\n<pre><code class=\"language-csharp\">classProgram\n{\n#region \u793a\u4f8b1\uff1a\u4e0d\u4f7f\u7528LINQ\u67e5\u8be2\u6570\u7ec4\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };\nList&lt;int&gt; list=newList&lt;int&gt;();\nforeach (intiteminnums)\n{\n    if (item%2!=0)\n    list.Add(item);\n}\nlist.Sort();\nlist.Reverse();\nforeach (intiteminlist)\n{\nConsole.WriteLine(item);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b2\uff1a\u4f7f\u7528LINQ\u6280\u672f\u67e5\u8be2\u6570\u7ec4\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };\nvarlist=fromnuminnums\nwherenum%2!=0\norderbynumdescending\nselectnum;\nforeach (intiteminlist)\n{\nConsole.WriteLine(item);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b3\uff1a\u6269\u5c55\u65b9\u6cd5Select()\u5e94\u7528\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };\nvarlist=nums.Select(item=&gt;item*item);\nforeach (intiteminlist)\n{\nConsole.WriteLine(item);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b4\uff1a\u6269\u5c55\u65b9\u6cd5Where()\u5e94\u7528\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };\nvarlist=nums\n.Where(item =&gt; item % 2 == 0)\n.Select(i =&gt; i * i);\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b5\uff1a\u6269\u5c55\u65b9\u6cd5OrderBy()\u5e94\u7528\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };\nvarlist=nums\n.Where(item =&gt; item % 2 == 0)\n.Select(i =&gt; i * i)\n.OrderBy(item =&gt; item);\nforeach (intiinlist)\n{\nConsole.WriteLine(i);\n}\nConsole.ReadLine();\n}\nstaticvoidMain(string[] args)\n{\nstring[] nums= { &quot;\u5f20\u52c7&quot;, &quot;\u738b\u7426&quot;, &quot;\u5218\u9759&quot;, &quot;\u8d75\u946b\u946b&quot;,\n&quot;\u675c\u4e3d&quot;, &quot;\u9a6c\u4fca\u624d&quot;, &quot;\u90a3\u82f1&quot;, &quot;\u6210\u9f99&quot;, };\nvarlist=nums\n.Where(item =&gt; item.Length == 2)\n.Select(item =&gt; item)\n.OrderByDescending(item =&gt; item.Substring(0, 1));\nforeach (stringiteminlist)\n{\nConsole.WriteLine(item);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b6\uff1a\u6269\u5c55\u65b9\u6cd5GroupBy()\u5e94\u7528\nstaticvoidMain(string[] args)\n{\nstring[] nums= { &quot;\u5f20\u52c7&quot;, &quot;\u738b\u7426&quot;, &quot;\u5218\u9759&quot;, &quot;\u8d75\u946b\u946b&quot;,\n&quot;\u675c\u4e3d&quot;, &quot;\u9a6c\u4fca\u624d&quot;, &quot;\u90a3\u82f1&quot;, &quot;\u6210\u9f99&quot;,&quot;\u738b\u4e3d&quot;, &quot;\u675c\u5b87&quot;,&quot;\u9a6c\u6653&quot;,&quot;\u5218\u4e3d&quot;,&quot;\u9a6c\u5927\u54c8&quot;,};\nvarlist=nums\n.Where(item =&gt; item.Length == 2)\n.Select(item =&gt; item)\n.GroupBy(item =&gt; item.Substring(0, 1));\nforeach (vargroupIteminlist)\n{\nConsole.WriteLine(&quot;-------------------&quot;);\nConsole.WriteLine(&quot;\u5206\u7ec4\u5b57\u6bb5\uff1a{0}&quot;, groupItem.Key);\nforeach (varitemingroupItem)\n{\nConsole.WriteLine(item);\n}\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b7\uff1a\u65ad\u70b9\u8c03\u8bd5LINQ\u7684\u67e5\u8be2\u65f6\u673a\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };\nvarlist=nums\n.Where(item =&gt; item % 2 == 0)\n.Select(item =&gt; item * item)\n.OrderBy(item =&gt; item);\nforeach (intiinlist)\n{\nConsole.WriteLine(i);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b8\uff1a\u67e5\u8be2\u7684\u7acb\u5373\u6267\u884c\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };\nvarlist=nums\n.Where(item =&gt; item % 2 == 0)\n.Select(item =&gt; item * item)\n.OrderBy(item =&gt; item)\n.Count();\nConsole.WriteLine(list.ToString ());\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b9\uff1afrom\u5b50\u53e5\u7684\u7b80\u5355\u4f7f\u7528\nstaticvoidMain(string[] args)\n{\nArrayListvalues=newArrayList();\nfor (inti=0; i&lt;10; i++) { values.Add(i); }\nvarlist=fromintiteminvalues\nwhereitem%2==0\nselectitem;\nforeach (intiteminlist) { Console.WriteLine(item); }\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b10\uff1a\u590d\u5408from\u5b50\u53e5\u7684\u4f7f\u7528\nstaticvoidMain(string[] args)\n{\nStudentobj1=newStudent()\n{\nStuId=1001,\nStuName=&quot;\u5b66\u54581&quot;,\nScoreList=newList&lt;int&gt;() { 90, 78, 54 }\n};\nStudentobj2=newStudent()\n{\nStuId=1002,\nStuName=&quot;\u5b66\u54582&quot;,\nScoreList=newList&lt;int&gt;() { 95, 88, 90 }\n};\nStudentobj3=newStudent()\n{\nStuId=1003,\nStuName=&quot;\u5b66\u54583&quot;,\nScoreList=newList&lt;int&gt;() { 79, 76, 89 }\n};\n\u5c06\u5b66\u5458\u5c01\u88c5\u5230\u96c6\u5408\u4e2d\nList&lt;Student&gt; stuList=newList&lt;Student&gt;() { obj1, obj2, obj3 };\n\u67e5\u8be2\u6210\u7ee9\u5305\u542b95\u5206\u4ee5\u4e0a\u7684\u5b66\u5458\nvarresult=fromstuinstuList\nfromscoreinstu.ScoreList\nwherescore&gt;=95\nselectstu;\n\u663e\u793a\u67e5\u8be2\u7ed3\u679c\nforeach (variteminresult)\n{\nConsole.WriteLine(item.StuName);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b11\uff1a\u591a\u4e2afrom\u5b50\u53e5\u67e5\u8be2\u7684\u4f7f\u7528\nstaticvoidMain(string[] args)\n{\nStudentobj1=newStudent() { StuId=1001, StuName=&quot;\u5b66\u54581&quot; };\nStudentobj2=newStudent() { StuId=1009, StuName=&quot;\u5b66\u54589&quot; };\nStudentobj3=newStudent() { StuId=1012, StuName=&quot;\u5b66\u545812&quot; };\nStudentobj4=newStudent() { StuId=1003, StuName=&quot;\u5b66\u54583&quot; };\nStudentobj5=newStudent() { StuId=1019, StuName=&quot;\u5b66\u545819&quot; };\nStudentobj6=newStudent() { StuId=1006, StuName=&quot;\u5b66\u54586&quot; };\nList&lt;Student&gt; stuList1=newList&lt;Student&gt;() { obj1, obj2, obj3 };\nList&lt;Student&gt; stuList2=newList&lt;Student&gt;() { obj4, obj5, obj6 };\n\u67e5\u8be2\u5b66\u597d\u5927\u4e8e1010\u7684\u5b66\u5458\nvarresult=fromstu1instuList1\nwherestu1.StuId&gt;=1010\nfromstu2instuList2\nwherestu2.StuId&gt;=1010\nselectnew { stu1, stu2 };\n\u663e\u793a\u67e5\u8be2\u7ed3\u679c\nforeach (variteminresult )\n{\nConsole.WriteLine(item.stu1.StuName+&quot; &quot;+item.stu1.StuId);\nConsole.WriteLine(item.stu2.StuName+&quot; &quot;+item.stu2.StuId);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b12\uff1a\u805a\u5408\u51fd\u6570Count\nstaticvoidMain(string[] args)\n{\nStudentobj1=newStudent() { StuId=1001, StuName=&quot;\u5b66\u54581&quot; };\nStudentobj2=newStudent() { StuId=1009, StuName=&quot;\u5b66\u54589&quot; };\nStudentobj3=newStudent() { StuId=1012, StuName=&quot;\u5b66\u545812&quot; };\nStudentobj4=newStudent() { StuId=1003, StuName=&quot;\u5b66\u54583&quot; };\nStudentobj5=newStudent() { StuId=1019, StuName=&quot;\u5b66\u545819&quot; };\nStudentobj6=newStudent() { StuId=1006, StuName=&quot;\u5b66\u54586&quot; };\nList&lt;Student&gt; stuList=newList&lt;Student&gt;() { obj1, obj2, obj3, obj4, obj5, obj6 };\nvarcount1= (fromcinstuList\nwherec.StuId&gt;1010\nselectc).Count();\nvarcount2=stuList.Where(c=&gt;c.StuId&gt;1010).Count();\nConsole.WriteLine(&quot;count1={0} count2={1}&quot;,count1,count2);\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b13\uff1a\u805a\u5408\u51fd\u6570Max\u3001Min\u3001Average\nstaticvoidMain(string[] args)\n{\nStudentobj1=newStudent() { StuId=1001, Age=22, StuName=&quot;\u5b66\u54581&quot; };\nStudentobj2=newStudent() { StuId=1009, Age=21, StuName=&quot;\u5b66\u54589&quot; };\nStudentobj3=newStudent() { StuId=1012, Age=25, StuName=&quot;\u5b66\u545812&quot; };\nStudentobj4=newStudent() { StuId=1003, Age=23, StuName=&quot;\u5b66\u54583&quot; };\nStudentobj5=newStudent() { StuId=1019, Age=27, StuName=&quot;\u5b66\u545819&quot; };\nStudentobj6=newStudent() { StuId=1006, Age=24, StuName=&quot;\u5b66\u54586&quot; };\nList&lt;Student&gt; stuList=newList&lt;Student&gt;() { obj1, obj2, obj3, obj4, obj5, obj6 };\nvarmaxAge= (fromsinstuList\nselects.Age).Max();\nvarminAge=stuList\n.Select(s =&gt; s.Age).Min();\nvaravgAge= (fromsinstuList\nselects.Age).Average();\nvarsumAge= (fromsinstuList\nselects.Age).Sum();\nConsole.WriteLine(&quot;maxAge={0} minAge={1} avgAge={2} sumAge={3}&quot;,\nmaxAge, minAge, avgAge,sumAge);\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b14\uff1a\u6392\u5e8f\u7c7bThenBy\u7684\u4f7f\u7528\nstaticvoidMain(string[] args)\n{\nStudentobj1=newStudent() { StuId=1001, Age=22, StuName=&quot;\u5b66\u54581&quot; };\nStudentobj2=newStudent() { StuId=1009, Age=21, StuName=&quot;\u5b66\u54589&quot; };\nStudentobj3=newStudent() { StuId=1012, Age=25, StuName=&quot;\u5b66\u545812&quot; };\nStudentobj4=newStudent() { StuId=1003, Age=23, StuName=&quot;\u5b66\u54583&quot; };\nStudentobj5=newStudent() { StuId=1019, Age=27, StuName=&quot;\u5b66\u545819&quot; };\nStudentobj6=newStudent() { StuId=1006, Age=24, StuName=&quot;\u5b66\u54586&quot; };\nList&lt;Student&gt; stuList=newList&lt;Student&gt;() { obj1, obj2, obj3, obj4, obj5, obj6 };\nvarstus1=fromsinstuList\norderbys.StuName, s.Age, s.StuId\nselects;\nvarstus2=stuList\n.OrderBy(s =&gt; s.StuName)\n.ThenBy(s =&gt; s.Age)\n.ThenBy(s =&gt; s.StuId)\n.Select(p =&gt; p);\nforeach (varsinstus1)\n{\nConsole.WriteLine(s.StuName);\n}\nConsole.WriteLine(&quot;----------------------&quot;);\nforeach (varsinstus2)\n{\nConsole.WriteLine(s.StuName);\n}\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b15\uff1a\u5206\u533a\u7c7b\u67e5\u8be2\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 2, 3, 4, 5, 6, 7, 8, 9 };\nvarlist1=nums.Skip(1).Take(3);\nvarlist2=nums.SkipWhile(i=&gt;i%3!=0)\n.TakeWhile(i =&gt; i % 2 != 0);\nforeach (variteminlist1) { Console.WriteLine(item); }\nConsole.WriteLine(&quot;------------&quot;);\nforeach (variteminlist2) { Console.WriteLine(item); }\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b16\uff1a\u96c6\u5408\u7c7b\u67e5\u8be2Distinct\nstaticvoidMain(string[] args)\n{\nint[] nums= { 1, 2, 2, 6, 5, 6, 7, 8, 8 };\nvarlist=nums.Distinct();\nforeach (variteminlist) { Console.WriteLine(item); }\nConsole.ReadLine();\n}\n#endregion\n#region \u793a\u4f8b17\uff1a\u751f\u6210\u7c7b\u67e5\u8be2\nstaticvoidMain(string[] args)\n{\nvarnums1=Enumerable.Range(1, 10);\nvarnums2=Enumerable.Repeat(&quot;LINQ best\uff01&quot;, 10);\nforeach (variteminnums1) { Console.WriteLine(item); }\nConsole.WriteLine(&quot;------------&quot;);\nforeach (variteminnums2) { Console.WriteLine(item); }\nConsole.ReadLine();\n}\n#endregion\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>test classProgram { #region \u793a\u4f8b1\uff1a\u4e0d\u4f7f\u7528LINQ\u67e5\u8be2\u6570\u7ec4 staticvoidM&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/www.kenneth.mobi\/?p=29\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-c","tag-linq"],"_links":{"self":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":4,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}