{"id":225,"date":"2020-09-21T06:15:24","date_gmt":"2020-09-21T06:15:24","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=225"},"modified":"2020-09-21T06:15:24","modified_gmt":"2020-09-21T06:15:24","slug":"linqhelper-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/09\/21\/linqhelper-cs\/","title":{"rendered":"LinqHelper.cs"},"content":{"rendered":"\n<p>Custom Linq Range Class<\/p>\n\n\n\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.Numerics;\npublic static class LinqHelper\n{\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of character values within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;char> Range(char from, char to, char step = '\\x0001')\n    {\n        var d = '\\x0';\n        while((char) (from + (uint) d) &lt; to)\n        {\n            yield return(char) (from + (uint) d);\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 8-bit signed numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;sbyte> Range(sbyte from, sbyte to, sbyte step = 1)\n    {\n        var d = 0;\n        while((sbyte) (from + d) &lt; to)\n        {\n            yield return(sbyte) (from + d);\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 8-bit unsigned numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;byte> Range(byte from, byte to, byte step = 1)\n    {\n        var d = 0;\n        while((byte) (from + d) &lt; to)\n        {\n            yield return(byte) (from + d);\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 16-bit signed numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;short> Range(short from, short to, short step = 1)\n    {\n        var d = 0;\n        while((short) (from + d) &lt; to)\n        {\n            yield return(short) (from + d);\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 16-bit unsigned numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;ushort> Range(ushort from, ushort to, ushort step = 1)\n    {\n        var d = 0;\n        while((ushort) (from + d) &lt; to)\n        {\n            yield return(ushort) (from + d);\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 32-bit signed numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;int> Range(int from, int to, int step = 1)\n    {\n        var d = 0;\n        while(from + d &lt; to)\n        {\n            yield return from + d;\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 32-bit unsigned numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;uint> Range(uint from, uint to, uint step = 1)\n    {\n        var d = 0u;\n        while(from + d &lt; to)\n        {\n            yield return from + d;\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 64-bit signed numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;long> Range(long from, long to, long step = 1)\n    {\n        var d = 0l;\n        while(from + d &lt; to)\n        {\n            yield return from + d;\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of 64-bit unsigned numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;ulong> Range(ulong from, ulong to, ulong step = 1)\n    {\n        var d = 0ul;\n        while(from + d &lt; to)\n        {\n            yield return from + d;\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of arbitrarily large signed numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;BigInteger> Range(BigInteger from, BigInteger to, BigInteger step)\n    {\n        var count = to - from;\n        for(var i = BigInteger.Zero; i &lt; count; i = i + step)\n            yield return from + i;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of arbitrarily large floating point precision numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;BigRational> Range(BigRational from, BigRational to, BigRational step)\n    {\n        if(step == 0)\n            step = 1;\n        var count = to - from;\n        for(BigRational i = 0; i &lt; count; i = +step)\n            yield return from + i;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of single precision floating-point numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;float> Range(float from, float to, float step = 1)\n    {\n        var d = 0f;\n        while(from + d &lt; to)\n        {\n            yield return from + d;\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of double precision floating-point numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;double> Range(double from, double to, double step = 1)\n    {\n        var d = 0d;\n        while(from + d &lt; to)\n        {\n            yield return from + d;\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of decimal numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;decimal> Range(decimal from, decimal to, decimal step = 1)\n    {\n        decimal d = 0;\n        while(from + d &lt; to)\n        {\n            yield return from + d;\n            d += step;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Generates a sequence of DateTime numbers within a specified range starting at\n    \/\/\/     'form' through 'to' with a given step value in days.\n    \/\/\/ &lt;\/summary>\n    public static IEnumerable&lt;DateTime> Range(DateTime from, DateTime to, double step = 1)\n    {\n        var d = from;\n        while(d &lt; to)\n        {\n            yield return d;\n            d = d.AddDays(step);\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Custom Linq Range Class<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[116,117],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/225"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/225\/revisions\/226"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}