Monday, September 6, 2010

LINQ - Linq to Object Part 4 Set Operator

Linq to Object Part 3

Step 1
Create a Web Project.

Step 2
Add a gridview control on page,it is look like this


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
    <asp:GridView ID="gvSet" runat="server">
    </asp:GridView>
    
    </div>
    </form>
</body>
</html>

Step 3
Distinct
In default class [default.aspx.cs] write a method for to get distinct value from array.it is look like this

/// <summary>
    /// Get the Distinct value
    /// </summary>
    private void Distinct()
    {
        try
        {
            int[] Number = { 1, 2, 2, 3, 4, 4, 5, 5, 5, 6, 6, 7, 8, 9, 9 };

            var DistinctValue = (from D in Number
                                 select D).Distinct();

            gvSet.DataSource = DistinctValue;
            gvSet.DataBind();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }
The Distinct operator is used to eliminate duplicate elements from a sequence.

Step 4
Union
In default class [default.aspx.cs] write a method for combine the two array value using Union.it is look like this
 /// <summary>
    /// Combine the two array value using Union
    /// </summary>
    private void Union()
    {
        try
        {
            int[] NumbersA = { 0, 2, 4, 5, 6, 8, 9 };
            int[] NumbersB = { 1, 3, 5, 7, 8 };

            var VarNumbersA = from A in NumbersA
                              select A;

            var VarNumbersB = from B in NumbersB
                              select B;

            var VarUnion = VarNumbersA.Union(VarNumbersB);

            gvSet.DataSource = VarUnion;
            gvSet.DataBind();   
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

The Union operator is used to combine the objects of two collections, but the output contains only distinct objects from both the collections.

Step 5
Except
In default class [default.aspx.cs] write a method for to get Numbers in first array but not in second array by using Except.it is look like this
 /// <summary>
    /// Numbers in first array but not in second array by using Except
    /// </summary>
    private void Except()
    {
        try
        {
            int[] NumbersA = { 0, 2, 4, 5, 6, 8, 9 };
            int[] NumbersB = { 1, 3, 5, 7, 8 };

            var VarNumberA = from A in NumbersA
                             select A;

            var VarNumberB = from B in NumbersB
                             select B;

            var VarExcept = VarNumberA.Except(VarNumberB);

            gvSet.DataSource = VarExcept;
            gvSet.DataBind();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

The Except operator is used to find the objects that are present in the first collection but not present in the second collection.

Step 6
Intersect
In default class [default.aspx.cs] write a method for to get common numbers shared by both arrays by using Intersect.it is look like this

 /// <summary>
    /// Common numbers shared by both arrays by using Intersect
    /// </summary>
    private void Intersect()
    {
        try
        {
            int[] NumbersA = { 0, 2, 4, 5, 6, 8, 9 };
            int[] NumbersB = { 1, 3, 5, 7, 8 };

            var VarNumberA = from A in NumbersA
                             select A;

            var VarNumberB = from B in NumbersB
                             select B;

            var VarIntersect = VarNumberA.Intersect(VarNumberB);

            gvSet.DataSource = VarIntersect;
            gvSet.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }
The Intersect operator is used to find the objects which are common in both the collections and outputs the same.

Full Code behind
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack == false)
            {
                //// Get Distinct value
                //  Distinct(); 

                //// Union value
                //Union(); 

                //// Except
                //Except(); 

                ////Intersect
                Intersect();

            }
        }
        catch (Exception)
        { }
    }

    #region Methods
    /// <summary>
    /// Get the Distinct value
    /// </summary>
    private void Distinct()
    {
        try
        {
            int[] Number = { 1, 2, 2, 3, 4, 4, 5, 5, 5, 6, 6, 7, 8, 9, 9 };

            var DistinctValue = (from D in Number
                                 select D).Distinct();

            gvSet.DataSource = DistinctValue;
            gvSet.DataBind();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

    /// <summary>
    /// Combine the two array value using Union
    /// </summary>
    private void Union()
    {
        try
        {
            int[] NumbersA = { 0, 2, 4, 5, 6, 8, 9 };
            int[] NumbersB = { 1, 3, 5, 7, 8 };

            var VarNumbersA = from A in NumbersA
                              select A;

            var VarNumbersB = from B in NumbersB
                              select B;

            var VarUnion = VarNumbersA.Union(VarNumbersB);

            gvSet.DataSource = VarUnion;
            gvSet.DataBind();   
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

    /// <summary>
    /// Numbers in first array but not in second array by using Except
    /// </summary>
    private void Except()
    {
        try
        {
            int[] NumbersA = { 0, 2, 4, 5, 6, 8, 9 };
            int[] NumbersB = { 1, 3, 5, 7, 8 };

            var VarNumberA = from A in NumbersA
                             select A;

            var VarNumberB = from B in NumbersB
                             select B;

            var VarExcept = VarNumberA.Except(VarNumberB);

            gvSet.DataSource = VarExcept;
            gvSet.DataBind();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }
    /// <summary>
    /// Common numbers shared by both arrays by using Intersect
    /// </summary>
    private void Intersect()
    {
        try
        {
            int[] NumbersA = { 0, 2, 4, 5, 6, 8, 9 };
            int[] NumbersB = { 1, 3, 5, 7, 8 };

            var VarNumberA = from A in NumbersA
                             select A;

            var VarNumberB = from B in NumbersB
                             select B;

            var VarIntersect = VarNumberA.Intersect(VarNumberB);

            gvSet.DataSource = VarIntersect;
            gvSet.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

    #endregion
}

Run the project

Download
Download Souce Code

No comments:

Post a Comment