Sunday, September 19, 2010

LINQ - Linq to Object Part 5 Aggregate Operators

Linq to Object Part 4

Step 1
Create a web project.

Step 2
Create a AggregateOperators static class in App_Code folder,it is look like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Demo of Linq Aggregator Operators
/// </summary>
public static class AggregateOperators
{

    #region Methods

    /// <summary>
    /// Count the number of array
    /// </summary>
    /// <returns>int</returns>
    public static int CountData()
    {
        try
        {
            int[] Number = { 2, 5, 3, 7, 8 };

            var Query = (from Q in Number
                         select Q).Count<int>() ;

            return Query; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

    /// <summary>
    /// Sum of the number of array
    /// </summary>
    /// <returns>int</returns>
    public static int SumData()
    {
        try
        {
            int[] Number = { 3, 5, 2, 10, 4, 6, 9 };

            var Query = (from Q in Number
                         select Q).Sum();

            return Query; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

    #endregion

    /// <summary>
    /// get the minimum number of array
    /// </summary>
    /// <returns>int</returns>
    public static int MinData()
    {
        try
        {
            int[] Number = { 4,7,3,8,6,2,6,1,5};

            var Query = (from Q in Number
                         select Q).Min();

            return Query; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);   
        }
    }

    /// <summary>
    /// get the maximum number of array
    /// </summary>
    /// <returns>int</returns>
    public static int MaxData()
    {
        try
        {
            int[] Number = { 4, 7, 3, 8, 6, 2, 6, 1, 5 };

            var Query = (from Q in Number select Q).Max();

            return Query;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    /// <summary>
    /// Get the average number of array
    /// </summary>
    /// <returns>int</returns>
    public static int AverageData()
    {
        try
        {
            int[] Number = { 4, 7, 3, 8, 6, 2, 6, 1, 5 };

            var Query = (from Q in Number select Q).Average(); 

            return (int)Query;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

}

Step 3
Call the above methods in Page Load event,it is look like this



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Response.Write("Count :" + AggregateOperators.CountData().ToString() + "</br>");
            Response.Write("Sum :" + AggregateOperators.SumData().ToString() + "</br>");
            Response.Write("Min :" + AggregateOperators.MinData().ToString() + "</br>");
            Response.Write("Max :" + AggregateOperators.MaxData().ToString() + "</br>");
            Response.Write("Average :" + AggregateOperators.AverageData().ToString() + "</br>");  
             
        }
        catch (Exception)
        { 
        }
    }
}

Run the project

Download
Download Source Code

 

No comments:

Post a Comment