Search

Saturday, April 26, 2014
0 comments

How to use LINQ Set Operators in ASP.NET

7:50 PMSaturday, April 26, 2014

Introduction

The Set operators in LINQ refer to the query operations that produce a result set. The result is based on the presence or absence of the equivalent elements that are present within the same or separate collection. The Distinct, Union, Intersect, and Except clauses are classified as the Set operators. The Distinct clause removes duplicate values from a collection.
The syntax of the Distinct clause is:
For C#
public static IEnumerable<T> Distinct<T>( this IEnumerabIe<T> source);

The Union clause returns the union of two sets. The result elements are the unique elements that are common in the two sets.
The syntax of the Union clause is:
For C#
public static IEnumerabIe<T> Union<T>( this IEnumerabIe<T> source1, IEnumerabIe<T> source2);

The Intersect clause returns the set intersection. The elements appear in each of the two collections.
The syntax of the Intersect clause is:
For C#
public static IEnumerabIe<T> Intersect<T>( this IEnumerabIe<T> source1, IEnumerabIe<T> source2);
The Except clause returns the set difference. The result of the Except clause returns the elements of one collection that do not appear in the second collection.
The syntax of the Except clause is:
For C#
public static 1Enumerable<T> Except<T>( this 1Enumerable<T> sourcel, IEnumerab1e<T> source2);

lets take an simple example

Source Code
<form id="form1" runat="server">
    <div>
    
    </div>
    <asp:ListBox ID="ListBox1" runat="server" Height="137px" Width="221px">
    </asp:ListBox>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="common word" />
    </form>
Code behind
protected void Button1_Click(object sender, EventArgs e)
    {
        string[] fruits = { "apple", "mango", "banana" };
        string[] wds = { "my world", "mango", "operation" };
        var common = fruits.Intersect(wds);
        ListBox1.Items.Add("common in both array");
        foreach (var item in common)
        {
            ListBox1.Items.Add(item);
        }
    }
Code Generate the following output
How to use LINQ Set Operators in ASP.NET

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top