{"id":3257,"date":"2020-01-24T14:06:44","date_gmt":"2020-01-24T13:06:44","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3257"},"modified":"2022-07-25T08:33:24","modified_gmt":"2022-07-25T07:33:24","slug":"c-async-selectforeach-with-yield-return","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-async-selectforeach-with-yield-return\/","title":{"rendered":"C# Async Select+ForEach with yield-return"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;\n\nnamespace AsyncTester\n{\n    class Program\n    {\n        public static async Task Main()\n        {\n            await GetInts()\n            .AsyncSelect(x => {\n                Console.WriteLine($\"Selecting {x}\");\n                return (x * 2).ToString(); \/\/ even convert type\n            })\n            .AsyncForeach(x => Console.WriteLine(x));\n\n\n            \/\/ C# 8 version, how to select\/map data?\n            var enumerable = GetIntsAsync();\n            await foreach (var i in enumerable)\n            {\n                Console.WriteLine(i);\n            }\n        }\n\n        \/\/ C# 8 version\n        public static async IAsyncEnumerable&lt;int> GetIntsAsync()\n        {\n            foreach (var item in Enumerable.Range(0, 10)) { \n                await Task.Delay(100);\n                yield return item;\n            }\n        }\n\n        public static IEnumerable&lt;Task&lt;int>> GetInts()\n        {\n            foreach (var item in Enumerable.Range(0, 10))\n                yield return Task.FromResult(item);\n        }\n    }\n\n    public static class AsyncHelper\n    {\n        public static async Task AsyncForeach&lt;T>(this IEnumerable&lt;Task&lt;T>> data, Action&lt;T> action)\n        {\n            foreach (var item in data)\n                action(await item.ConfigureAwait(false));\n        }\n        public static IEnumerable&lt;Task&lt;U>> AsyncSelect&lt;T, U>(this IEnumerable&lt;Task&lt;T>> data, Func&lt;T, U> transform)\n        {\n            foreach (var item in data)\n                yield return Task.Run(async () => transform(await item));\n        }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-3257","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3257"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3257\/revisions"}],"predecessor-version":[{"id":3263,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3257\/revisions\/3263"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}