# Sum and Except Operations Using 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:

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">

<style type="text/css">

.grid

{

margin-top: 50px;

}

</style>

<title></title>

<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"

Width="184px">

<AlternatingRowStyle BackColor="PaleGoldenrod" />

<FooterStyle BackColor="Tan" />

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

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

<sortedascendingcellstyle backcolor="#FAFAE7" />

<sorteddescendingcellstyle backcolor="#E1DB9C" />

</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:

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

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:

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">

<style type="text/css">

.grid

{

margin-top: 50px;

}

</style>

<title></title>

<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"

<AlternatingRowStyle BackColor="PaleGoldenrod" />

<FooterStyle BackColor="Tan" />

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

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

<sortedascendingcellstyle backcolor="#FAFAE7" />

<sorteddescendingcellstyle backcolor="#E1DB9C" />

</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:

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