{"id":831,"date":"2021-09-12T20:31:10","date_gmt":"2021-09-12T12:31:10","guid":{"rendered":"http:\/\/www.wayln.com\/?p=831"},"modified":"2021-09-12T20:31:10","modified_gmt":"2021-09-12T12:31:10","slug":"%e5%bc%82%e6%ad%a5%e6%b5%81-%e5%af%b9%e5%bc%82%e6%ad%a5%e6%b5%81%e4%bd%bf%e7%94%a8linq","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=831","title":{"rendered":"\u5f02\u6b65\u6d41\u2014\u2014\u5bf9\u5f02\u6b65\u6d41\u4f7f\u7528LINQ"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u95ee\u9898<\/a><\/li><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_1\">2<\/span> \u89e3\u51b3\u65b9\u6848<\/a><\/li><li><a href=\"#i-3\"><span class=\"toc_number toc_depth_1\">3<\/span> \u8ba8\u8bba<\/a><\/li><\/ul><\/div>\n<h1><span id=\"i\">\u95ee\u9898<\/span><\/h1>\n<p>\u5047\u8bbe\u8981\u4f7f\u7528\u7ecf\u8fc7\u59a5\u5584\u5b9a\u4e49\u4ee5\u53ca\u5b8c\u5584\u6d4b\u8bd5\u7684\u8fd0\u7b97\u7b26\uff0c\u6765\u5904\u7406\u5f02\u6b65\u6d41\u3002<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>IEnumerable&lt;T&#62;\u5177\u6709LINQ to Objects,\u800cIObservable&lt;T&#62;\u5177\u6709LINQ to Events.\u4e24\u8005\u90fd\u6709\u6269\u5c55\u65b9\u6cd5\u5e93\u3002IAsyncEnumerable&lt;T&#62;\u4e5f\u5177\u5907LINQ\u652f\u6301\uff0c\u5305\u542b\u4e8eAsystem.Linq.Async NuGet\u5305\u4e2d<\/p>\n<pre><code class=\"language-CSharp line-numbers\">IAsyncEnumerable&lt;int&gt; values=SlowRange().WhereAwait(\n    async value=&gt;{\n        \/\/\u6267\u884c\u67d0\u4e9b\u5f02\u6b65\u64cd\u4f5c\n        await Task.Delay(10);\n        return value%2 ==0;\n    }\n);\nawait foreach(int result in values)\n{\n    Console.WriteLine(result);\n}\n\nasync IAsyncEnumerable&lt;int&gt; SlowRange()\n{\n    for(int i=0;i!=10;i++)\n    {\n        await Task.Delay(i*100);\n        yield return i;\n    }\n}\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u56e0\u4e3a\u5f02\u6b65\u6d41\u57fa\u4e8e\u62c9\u5f0f\u6a21\u578b\uff0c\u6240\u4ee5\u4ed6\u4e0d\u50cf\u53ef\u89c2\u5bdf\u5bf9\u8c61\u90a3\u6837\u5177\u6709\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u8fd0\u7b97\u7b26\uff0c\u5728\u4e00\u4e0d\u7559\u79cd\uff0cThrottle\u548cSample\u5e76\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\uff0c\u8fd9\u662f\u56e0\u4e3a\u5143\u7d20\u662f\u6309\u9700\u4ece\u5f02\u6b65\u6d41\u4e2d\u62c9\u53d6\u51fa\u6765\u7684\u3002<br \/>\n\u53ef\u4ee5\u5bf9\u4efb\u610f\u7684IEnumerable&lt;T&#62;\u8c03\u7528ToAsyncEnumerable(),\u7136\u540e\u4fbf\u53ef\u4ee5\u5728\u5f02\u6b65\u6d41\u63a5\u53e3\u4e2d\u4f7f\u7528WhereAwait\u3001SelectAwait\u4ee5\u53ca\u5176\u4ed6\u652f\u6301\u5f02\u6b65\u6d41\u59d4\u6258\u7684\u8fd0\u7b97\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u5047\u8bbe\u8981\u4f7f\u7528\u7ecf\u8fc7\u59a5\u5584\u5b9a\u4e49\u4ee5\u53ca\u5b8c\u5584\u6d4b\u8bd5\u7684\u8fd0\u7b97\u7b26\uff0c\u6765\u5904\u7406\u5f02\u6b65\u6d41\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,41,2],"tags":[],"class_list":["post-831","post","type-post","status-publish","format-standard","hentry","category-c","category-41","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=831"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":832,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/831\/revisions\/832"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}