LINQ in C# - Non-Deferred Operator Examples - Aggregating

Count

Count - Type 1 Example 1

         string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

                    int Count = names.Count(); // 4
        

Count - Type 2 with Condition Example 1

string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

                        int Count = names.Count(x=> x.Length==4); // 2

LongCount

The LongCount operator returns the number of elements in the input sequence as a long.
string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

                        long Count = names.LongCount(x=> x.Length==4); // 2

Sum

The Sum operator cannot have conditions
nt[] names = {1, 2, 3 , 4};

                        int sum= names.Sum()// 10

Min/Max

The Min/Max operator cannot have conditions

Min/Max - Type 1 - Example


                         int[] numbers = {1, 2, 3 , 4};
                        string[] names = { "Person A","Person B", "Person C", "Person D" };     

                        string minString = names.Min(); // Person A
                        Console.WriteLine(minString);
                        int minInt = numbers.Min(); // 1
                        Console.WriteLine(minInt);

Min/Max - Type 2 with Property Selector- Example

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

                        string MinCustomerName = Customers.Min(x=> x.Name);
                        Console.WriteLine(MinCustomerName);
//Output
ABC Private Limited

Average

The Average operator returns the average of numeric values contained in the elements of the input sequence.

Average - Type 1 - Example 1

Average returns double return type
 
int[] numbers = {1, 2, 3 , 4};
                        double Avg = numbers.Average();
                        Console.WriteLine(Avg);  // 2.5

Average - Type 2 with Property Selector- Example

class Order
    {
        public int OrderID { get; set; }
        public int CustomerID { get; set; }
        public DateTime OrderDate { get; set; }
        public decimal Total { get; set; }     
    }

List<Order> Orders = new List<Order> {
                                    new Order(){ OrderID=1, OrderDate=new DateTime(2020,10,1), CustomerID=1, Total=1025.24M },
                                    new Order(){ OrderID=2, OrderDate=new DateTime(2020,4,1), CustomerID=1, Total=1000.24M },
                                    new Order(){ OrderID=3, OrderDate=new DateTime(2020,4,1), CustomerID=2, Total=1140.24M },
                                    new Order(){ OrderID=4, OrderDate=new DateTime(2020,2,1), CustomerID=2, Total=1160.24M },
                                    new Order(){ OrderID=5, OrderDate=new DateTime(2020,1,1), CustomerID=3, Total=1567.24M },
                                 };

decimal OrderAverage = Orders.Average(x=> x.Total);
                        Console.WriteLine(OrderAverage);

Aggregate

The easiest-to-understand definition of Aggregate is that it performs an operation on each element of the list taking into account the operations that have gone before.

Aggregate - Type 1 Example 1 Summing numbers

int[] numbers = {1, 2, 3 , 4};                   
                        var sum = numbers.Aggregate((a, b) => a + b); //output: 10 (1 + 2 + 3 + 4)
                        Console.WriteLine(sum);
This adds 1 and 2 to make 3. Then adds 3 (result of previous) and 3 (next element in sequence) to make 6. Then adds 6 and 4 to make 10.

Aggregate - Type 1 Example 2 create a csv from an array of strings

var chars = new[] { "a", "b", "c", "d" };
            var csv = chars.Aggregate((a, b) => a + ',' + b);
            Console.WriteLine(csv); // Output a,b,c,d

Aggregate - Type 2 Example 1 Summing numbers with a seed

int[] numbers = {1, 2, 3 , 4};                   
                        var sum = numbers.Aggregate(10, (a, b) => a + b); //output: 20 = (10 + 1 + 2 + 3 + 4)
                        Console.WriteLine(sum);

Aggregate - Type 2 Example 2 with a seed

var chars = new[] { "a", "b", "c", "d" };
            var csv = chars.Aggregate(new StringBuilder(), (a, b) => {
                if (a.Length > 0)
                    a.Append(",");
                a.Append(b);
                return a;
            });
            Console.WriteLine(csv);
//Output
a,b,c,d

No comments:

Post a Comment

Framework Fundamentals - String - Comparing Strings

In comparing two values, the .NET Framework differentiates the concepts of equality comparison and order comparison . Equality compariso...