In this article you will learn about the following LINQ query operators:
- Distinct
- Except
- Intersect
- Union
The above LINQ commands are very useful on collections types. It helps to get desired output and data records very quickly.
Summary of Articles:
- Distinct To get unique records and entries from collections.
- Except To get only difference records and entries from given collections.
- Intersect To get only common records and entries from given collections.
- Union To get unique records and entries from given collections.
To achieve all above functionality we required System.Linq Namespae.
Now we will go through individually each and every topic in detail.
Distinct
To get non-duplicate records and to filter a collection for unique records.
Distinct is extension method of System.Linq. Without this namespace you getthe following error.
'System.Collections.Generic.IList<string>' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'System.Collections.Generic.IList<string>' could be found (are you missing a using directive or an assembly reference?
Above error will occur if you use DISTINCT command with LIST / ILIST without attaching System.Linq namespace on the page.
To be practical I had created one ASP.NET website project named: Linq Examples
Add a Linq To Sql Clases item in project.
As you click on ADD button, this dialog box appears. Click on Yes button on following alert box.
Drag and drop table(s) from Server Explorer to DBML window.
Distinct Example:
Add new WebForm called Distinct.aspx.
We will do this with the following table and table records.
Table Structure
- CREATE TABLE [dbo].[tblProducts](
- [ProductName] [nvarchar](50) NOT NULL
- ) ON [PRIMARY]
There is a total of 12 records in the above list.
Linq Query: (from a in db.tblProducts select a).Distinct();
Distinct.aspx code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Distinct.aspx.cs" Inherits="Distinct" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <p>All Records</p>
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- <p>Distinct Records</p>
- <asp:GridView ID="GridView2" runat="server"></asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Distinct.aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class Distinct : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- var db = new LinqExampleDataClassesDataContext();
-
- var AllValueResultSet = (from a in db.tblProducts select a);
- GridView1.DataSource = AllValueResultSet;
- GridView1.DataBind();
-
- var DistinctValueResultSet = (from a in db.tblProducts select a).Distinct();
- GridView2.DataSource = DistinctValueResultSet;
- GridView2.DataBind();
-
- }
- }
Output:
Except
Return the differential records set from given two collections.
It will check List records of XYZ records with List ABC return the differential records.
Except is extension method of System.Linq. Without this namespace you get error.
Except is helpful to compare the one collection object with other collection object and get the except value very quickly. Reduce our extra code writing efforts very smartly, otherwise we have to run ForEach loop and other things to get the result.
Add new web form called EXCEPT.ASPX
Except.Aspx code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class Except : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string[] AshishSubjects = {"Hindi","Marathi","Maths","Science","English","Computer"};
- string[] SuhanaSubjects = { "Hindi", "Marathi", "Maths", "Science", "French", "Value Education" };
-
- GridView1.DataSource = AshishSubjects;
- GridView1.DataBind();
-
- GridView2.DataSource = SuhanaSubjects;
- GridView2.DataBind();
-
- GridView3.DataSource = AshishSubjects.Except(SuhanaSubjects);
- GridView3.DataBind();
- }
- }
Except.Aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class Except : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string[] AshishSubjects = {"Hindi","Marathi","Maths","Science","English","Computer"};
- string[] SuhanaSubjects = { "Hindi", "Marathi", "Maths", "Science", "French", "Value Education" };
-
- GridView1.DataSource = AshishSubjects;
- GridView1.DataBind();
-
- GridView2.DataSource = SuhanaSubjects;
- GridView2.DataBind();
-
- GridView3.DataSource = AshishSubjects.Except(SuhanaSubjects);
- GridView3.DataBind();
- }
- }
Output
Intersect
To get common records and entries from two collections there is one kind of reverse effect of EXCEPT. This is very helpful when we want to check common values from one collection items with another collection items.
Intersect is extension method of System.Linq. Without this namespace you get error.
Add new web form called INTERSECT.ASPX
Intersect.Aspx code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Intersect.aspx.cs" Inherits="Intersect" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <p>Ashish Kalla Subjects</p>
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- <p>Suhana Kalla Subjects</p>
- <asp:GridView ID="GridView2" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- <p><b>Ashish and Suhana Common Subjects that is INTERSECT</b></p>
- <asp:GridView ID="GridView3" runat="server"></asp:GridView>
- </td>
-
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Intersect.Aspx .cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class Intersect : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string[] AshishSubjects = { "Hindi", "Marathi", "Maths", "Science", "English", "Computer" };
- string[] SuhanaSubjects = { "Hindi", "Marathi", "Maths", "Science", "French", "Value Education" };
-
- GridView1.DataSource = AshishSubjects;
- GridView1.DataBind();
-
- GridView2.DataSource = SuhanaSubjects;
- GridView2.DataBind();
-
- GridView3.DataSource = AshishSubjects.Intersect(SuhanaSubjects);
- GridView3.DataBind();
- }
- }
Output:
Union
To get non-duplicate records and entries from two collections.
Mixing two collections object items on the non-duplicate basis. It's very helpful and fast to get unique values from one collection items with another collection items.
Union is extension method of System.Linq. Without this namespace you get error.
Add new web form called UNION.ASPX
Union.Aspx code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Union.aspx.cs" Inherits="Union" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <p>Ashish Kalla Subjects</p>
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- <p>Suhana Kalla Subjects</p>
- <asp:GridView ID="GridView2" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- <p><b>Both Ashish and Suhana Unique Subjects that is Union</b></p>
- <asp:GridView ID="GridView3" runat="server"></asp:GridView>
- </td>
-
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Union.Aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class Union : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string[] AshishSubjects = { "Hindi", "Marathi", "Maths", "Science", "English", "Computer" };
-
- string[] SuhanaSubjects = { "Hindi", "Marathi", "Maths", "Science", "French", "Value Education" };
-
- GridView1.DataSource = AshishSubjects;
- GridView1.DataBind();
-
- GridView2.DataSource = SuhanaSubjects;
- GridView2.DataBind();
-
- GridView3.DataSource = AshishSubjects.Union(SuhanaSubjects);
- GridView3.DataBind();
-
-
- }
- }
Output
Read more articles on LINQ: