{"id":11517,"date":"2016-07-14T10:19:16","date_gmt":"2016-07-14T15:19:16","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=11517"},"modified":"2016-07-14T10:24:36","modified_gmt":"2016-07-14T15:24:36","slug":"c-example-split-an-array-into-multiple-smaller-arrays","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=11517","title":{"rendered":"C# Example: Split An Array into Multiple Smaller Arrays"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Example\">Example<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Example\"><h2>Example<\/h2><\/span>\n<p>* Use Lambda expression to extends Array class to include a new method called Split:<\/p>\n<pre lang=\"csharp\">\r\n    public static class MyArrayExtensions\r\n    {\r\n        \/\/\/ <summary>\r\n        \/\/\/ Splits an array into several smaller arrays.\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <typeparam name=\"T\">The type of the array.<\/typeparam>\r\n        \/\/\/ <param name=\"array\">The array to split.<\/param>\r\n        \/\/\/ <param name=\"size\">The size of the smaller arrays.<\/param>\r\n        \/\/\/ <returns>An array containing smaller arrays.<\/returns>\r\n        public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)\r\n        {\r\n            for (var i = 0; i < (float)array.Length \/ size; i++)\r\n            {\r\n                yield return array.Skip(i * size).Take(size);\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>* Test the new array class method:<\/p>\n<pre lang=\"csharp\">\r\n        [TestMethod]\r\n        public void TestSplit2()\r\n        {\r\n            int[] array1 = new int[100];\r\n            for (int i = 0; i < array1.Length; i++)\r\n            {\r\n                array1[i] = i;\r\n            }\r\n\r\n            \/\/ Split into smaller arrays of maximal 30 elements\r\n            IEnumerable<IEnumerable<int>> splited = array1.Split<int>(30);\r\n            int j = 0;\r\n            foreach (IEnumerable<int> s in splited){\r\n                j++;\r\n            }\r\n            log.InfoFormat(\"Splitted in to {0} smaller arrays.\", j);\r\n        }\r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/stackoverflow.com\/questions\/18986129\/c-splitting-an-array-into-n-parts\">C#: Splitting an array into n parts<\/a><br \/>\n* <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/bb397687.aspx\">Lambda Expressions (C# Programming Guide)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example * Use Lambda expression to extends Array class to include a new method called Split: public static class MyArrayExtensions { \/\/\/ \/\/\/ Splits an array into several smaller arrays. \/\/\/ \/\/\/ The type of the array. \/\/\/ The array &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=11517\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[702,556],"class_list":["post-11517","post","type-post","status-publish","format-standard","hentry","category-c","tag-array","tag-c"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2ZL","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11517"}],"version-history":[{"count":2,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11517\/revisions"}],"predecessor-version":[{"id":11520,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11517\/revisions\/11520"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}