Some Useful LINQ Query Operators

In this article you will learn about the following LINQ query operators:

  1. Distinct
  2. Except
  3. Intersect
  4. 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.

check error

To be practical I had created one ASP.NET website project named: Linq Examples

ASP.NET website project

Add a Linq To Sql Clases item in project.

Linq To Sql Clases

As you click on ADD button, this dialog box appears. Click on Yes button on following alert box.

alert box

Drag and drop table(s) from Server Explorer to DBML window.

drop table

Distinct Example:

Add new WebForm called Distinct.aspx.

WebForm

We will do this with the following table and table records.

Table Structure

  1. CREATE TABLE [dbo].[tblProducts](  
  2.     [ProductName] [nvarchar](50) NOT NULL  
  3. ON [PRIMARY]  
table

There is a total of 12 records in the above list.

Linq Query: (from a in db.tblProducts select a).Distinct();

Distinct.aspx code

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Distinct.aspx.cs" Inherits="Distinct" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <table>  
  13.                 <tr>  
  14.                     <td>  
  15.                         <p>All Records</p>  
  16.                         <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  17.                     </td>  
  18.                     <td style="padding-left:100px">  
  19.                         <p>Distinct Records</p>  
  20.                         <asp:GridView ID="GridView2" runat="server"></asp:GridView>  
  21.                     </td>  
  22.                 </tr>  
  23.             </table>  
  24.         </div>  
  25.     </form>  
  26. </body>  
  27. </html>  
Distinct.aspx.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class Distinct : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         var db = new LinqExampleDataClassesDataContext();  
  13.   
  14.         var AllValueResultSet = (from a in db.tblProducts select a);  
  15.         GridView1.DataSource = AllValueResultSet;  
  16.         GridView1.DataBind();  
  17.   
  18.         var DistinctValueResultSet = (from a in db.tblProducts select a).Distinct();  
  19.         GridView2.DataSource = DistinctValueResultSet;  
  20.         GridView2.DataBind();  
  21.   
  22.     }  
  23. }  
Output:

all record

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.

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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class Except : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.        string[] AshishSubjects = {"Hindi","Marathi","Maths","Science","English","Computer"};  
  13. string[] SuhanaSubjects = { "Hindi""Marathi""Maths""Science""French""Value Education" };  
  14.   
  15.         GridView1.DataSource = AshishSubjects;  
  16.         GridView1.DataBind();  
  17.   
  18.         GridView2.DataSource = SuhanaSubjects;  
  19.         GridView2.DataBind();  
  20.   
  21.         GridView3.DataSource = AshishSubjects.Except(SuhanaSubjects);  
  22.         GridView3.DataBind();  
  23.     }  
  24. }  
Except.Aspx.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class Except : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.        string[] AshishSubjects = {"Hindi","Marathi","Maths","Science","English","Computer"};  
  13. string[] SuhanaSubjects = { "Hindi""Marathi""Maths""Science""French""Value Education" };  
  14.   
  15.         GridView1.DataSource = AshishSubjects;  
  16.         GridView1.DataBind();  
  17.   
  18.         GridView2.DataSource = SuhanaSubjects;  
  19.         GridView2.DataBind();  
  20.   
  21.         GridView3.DataSource = AshishSubjects.Except(SuhanaSubjects);  
  22.         GridView3.DataBind();  
  23.     }  
  24. }  
Output

see result

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.

see error

Add new web form called INTERSECT.ASPX

Intersect.Aspx code 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Intersect.aspx.cs" Inherits="Intersect" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.     <table>  
  13.                     <tr>  
  14.                         <td>  
  15.                             <p>Ashish Kalla Subjects</p>  
  16.                             <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  17.                         </td>  
  18.                         <td style="padding-left:100px">  
  19.                             <p>Suhana Kalla Subjects</p>  
  20.                             <asp:GridView ID="GridView2" runat="server"></asp:GridView>  
  21.                         </td>  
  22.                         <td style="padding-left:100px">  
  23.                             <p><b>Ashish and Suhana Common Subjects that is INTERSECT</b></p>  
  24.                             <asp:GridView ID="GridView3" runat="server"></asp:GridView>  
  25.                         </td>  
  26.   
  27.                     </tr>  
  28.                 </table>  
  29.     </div>  
  30.     </form>  
  31. </body>  
  32. </html>  
Intersect.Aspx .cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class Intersect : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         string[] AshishSubjects = { "Hindi""Marathi""Maths""Science""English""Computer" };  
  13.         string[] SuhanaSubjects = { "Hindi""Marathi""Maths""Science""French""Value Education" };  
  14.   
  15.         GridView1.DataSource = AshishSubjects;  
  16.         GridView1.DataBind();  
  17.   
  18.         GridView2.DataSource = SuhanaSubjects;  
  19.         GridView2.DataBind();  
  20.   
  21.         GridView3.DataSource = AshishSubjects.Intersect(SuhanaSubjects);  
  22.         GridView3.DataBind();  
  23.     }  
  24. }  
Output:

run

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.

error

Add new web form called UNION.ASPX

Union.Aspx code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Union.aspx.cs" Inherits="Union" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.     <table>  
  13.                     <tr>  
  14.                         <td>  
  15.                             <p>Ashish Kalla Subjects</p>  
  16.                             <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  17.                         </td>  
  18.                         <td style="padding-left:100px">  
  19.                             <p>Suhana Kalla Subjects</p>  
  20.                             <asp:GridView ID="GridView2" runat="server"></asp:GridView>  
  21.                         </td>  
  22.                         <td style="padding-left:100px">  
  23.                             <p><b>Both Ashish and Suhana Unique Subjects that is Union</b></p>  
  24.                             <asp:GridView ID="GridView3" runat="server"></asp:GridView>  
  25.                         </td>  
  26.   
  27.                     </tr>  
  28.                 </table>  
  29.     </div>  
  30.     </form>  
  31. </body>  
  32. </html>  
Union.Aspx.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class Union : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         string[] AshishSubjects = { "Hindi""Marathi""Maths""Science""English""Computer" };  
  13.   
  14.         string[] SuhanaSubjects = { "Hindi""Marathi""Maths""Science""French""Value Education" };  
  15.   
  16.         GridView1.DataSource = AshishSubjects;  
  17.         GridView1.DataBind();  
  18.   
  19.         GridView2.DataSource = SuhanaSubjects;  
  20.         GridView2.DataBind();  
  21.   
  22.         GridView3.DataSource = AshishSubjects.Union(SuhanaSubjects);  
  23.         GridView3.DataBind();  
  24.   
  25.   
  26.     }  
  27. }  
Output

Output  

Read more articles on LINQ: