Reader Level:
Article

Sum and Except Operations Using LINQ

By Vijay Prativadi on Nov 05, 2012
Today, in this article you will see the most useful operations "sum" and "except" in LINQ.

 Introduction

 

Today, in this article you will see the most useful operations "sum" and "except" in LINQ.

 

Sum Operation Using LINQ

 

Question: What is sum operation?

In simple terms "It is used to project the result set with an addition operation for the requested data".

Step 1: Create a new "ASP.NET Web Application", as in:

Linq-sum-application.jpg

Step 2: The complete code of WebForm1.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LINQ_Sum_App.WebForm1" %>

 

<!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 id="Head1" runat="server">

    <style type="text/css">

        .grid

        {

            margin-top: 50px;

        }

    </style>

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Label ID="Label1" runat="server" Text="Sum Operation using LINQ" Font-Bold="true"

                            Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button1" runat="server" Text="Select Data" Font-Names="Verdana" Width="213px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:GridView ID="GridView1" runat="server" CssClass="grid" BackColor="LightGoldenrodYellow"

                            BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"

                            Width="184px">

                            <AlternatingRowStyle BackColor="PaleGoldenrod" />

                            <FooterStyle BackColor="Tan" />

                            <HeaderStyle BackColor="Tan" Font-Bold="True" />

                            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

                            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

                            <sortedascendingcellstyle backcolor="#FAFAE7" />

                            <sortedascendingheaderstyle backcolor="#DAC09E" />

                            <sorteddescendingcellstyle backcolor="#E1DB9C" />

                            <sorteddescendingheaderstyle backcolor="#C2A47B" />

                        </asp:GridView>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label2" runat="server" Text="Total Age:" Visible="false"></asp:Label>

                    </td>

                    <td>

                        <asp:Label ID="Label3" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="Large"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</html>

 

 Step 3: The complete code of WebForm1.aspx.cs looks like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

namespace LINQ_Sum_App

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            var query = from r in objEntities.Employee select r;

            GridView1.DataSource = query; GridView1.DataBind();

            var querySum = from r in objEntities.Employee select new { Age = r.Age };

            Label3.Text = query.Sum(p => p.Age).ToString();

            Label2.Visible = true;

        }

        #region Instance MembersCompanyEntities objEntities = new CompanyEntities();

        #endregion

    }

}

Step 4: The output of the application looks like this:

output-of-the-sum-application.jpg
 

Step 5: The data retrieved output of the application looks like this:

retrieved-output-of-the-sum-application.jpg

Except Operation using LINQ
 

Question: What is an "except" operation?

 

In simple terms "It is used to produce the result set for the records not found in the succeeding tables".

Step 1: Create a new "ASP.NET Web Application", as in:

Linq-Except-application.jpg
 

Step 2: The complete code of WebForm1.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LINQExceptApp.WebForm1" %>

 

<!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 id="Head1" runat="server">

    <style type="text/css">

        .grid

        {

            margin-top: 50px;

        }

    </style>

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Label ID="Label1" runat="server" Text="Except Operation using LINQ" Font-Bold="true"

                            Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button1" runat="server" Text="Select Data" Font-Names="Verdana" Width="213px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:GridView ID="GridView1" runat="server" CssClass="grid" BackColor="LightGoldenrodYellow"

                            BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">

                            <AlternatingRowStyle BackColor="PaleGoldenrod" />

                            <FooterStyle BackColor="Tan" />

                            <HeaderStyle BackColor="Tan" Font-Bold="True" />

                            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

                            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

                            <sortedascendingcellstyle backcolor="#FAFAE7" />

                            <sortedascendingheaderstyle backcolor="#DAC09E" />

                            <sorteddescendingcellstyle backcolor="#E1DB9C" />

                            <sorteddescendingheaderstyle backcolor="#C2A47B" />

                        </asp:GridView>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</body>

</html>

Step 3: The complete code of WebForm1.aspx.cs looks like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace LINQExceptApp

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            var queryEmp = from r in objEntities.Employee

                           select new

                               {

                                   FirstName = r.FirstName,

                                   LastName = r.LastName,

                                   Age = r.Age

                               };

            var queryTech = from p in objEntities.Techies

                            select new

                                {

                                    FirstName = p.FirstName,

                                    LastName = p.LastName,

                                    Age = p.Age

                                };

            var query = queryEmp.Except(queryTech);

            GridView1.DataSource = query;

            GridView1.DataBind();

        }

        #region Instance MembersCompanyEntities objEntities = new CompanyEntities();

        #endregion

    }

}

Step 4: The output of the application looks like this:

output-of-the-Except-application.jpg
 

Step 5: The data retrieved output of the application looks like this:

retrieved-output-of-the-Except-application.jpg

Vijay Prativadi

Hey Everyone, It's Vijay. I am SDE mostly up on Microsoft Technologies. If you are thinking to know what I do mostly when at work. I get a chance to work upon few Microsoft technologies. All the way along my core tech... Read more

COMMENT USING