{"id":539,"date":"2023-09-07T14:59:53","date_gmt":"2023-09-07T20:59:53","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=539"},"modified":"2023-09-08T09:09:12","modified_gmt":"2023-09-08T15:09:12","slug":"basic-example-of-aggregate-in-c-net","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/basic-example-of-aggregate-in-c-net\/","title":{"rendered":"Basic example of Aggregate in C# .Net"},"content":{"rendered":"\n<p>In C#, the <code>Aggregate<\/code> method is part of LINQ.<\/p>\n\n\n\n<p>It allows to perform an operation on all elements of a collection to accumulate a result. <\/p>\n\n\n\n<p>It applies a specified function to each element in the collection, along with an accumulator value, and returns a single aggregated result.<\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">using System;\nusing System.Linq;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static void Main()\n    {\n        List&lt;int> numbers = new List&lt;int> { 1, 2, 3, 4, 5 };\n\n        int product = numbers.Aggregate((accumulator, currentNumber) => accumulator * currentNumber);\n\n        Console.WriteLine(\"Product: \" + product);\n    }\n}\n<\/code><\/pre><\/div><\/div>\n\n\n\n<p>In this example, the <strong><code>accumulator<\/code> <\/strong>represents the accumulated result, and <strong><code>currentNumber<\/code> <\/strong>represents the current number in the iteration.<\/p>\n\n\n\n<p>The <code>Aggregate<\/code> method starts with the first element of the list as the initial <code><strong>accumulator<\/strong><\/code> value and then applies the <strong>lambda expression<\/strong> to each subsequent element in the list, accumulating the product.<\/p>\n\n\n\n<p>Another example: <\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">        public string BookTitlesAfter2015Concatenated()\n        {\n            return booksCollection\n            .Where(p => p.PublishedDate.Year > 2015)\n                .Aggregate(\"\", (BookTitles, next) =>\n                {\n                    if (BookTitles != string.Empty)\n                        BookTitles += \" - \" + next.Title;\n                    else\n                        BookTitles += next.Title;\n\n                    return BookTitles;\n                });\n        }<\/code><\/pre><\/div><\/div>\n\n\n\n<p>Basically, concatenates iteratively and stores the string (string in this case) into the variable BookTitles. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C#, the Aggregate method is part of LINQ. It allows to perform an operation on all elements of a collection to accumulate a result. It applies a specified function to each element in the collection, along with an accumulator value, and returns a single aggregated result. In this example, the accumulator represents the accumulated [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":456,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[51,32,97,7],"tags":[65,53,18,98,54],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/539"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=539"}],"version-history":[{"count":3,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/539\/revisions"}],"predecessor-version":[{"id":548,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/539\/revisions\/548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/456"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}