Let's play around with LINQ Queries in C#.NET


Today, we will play around using LINQ Queries in C#.NET effectively. Let's try to use some of the predefined methods of LINQ Queries and let's dig out and find how to write LINQ rather than traditional way of using normal Classes.

I think we are now good to go and do some practical session ASAP.

Accordingly, First and Foremost thing as per this article I have created simple table in SQL Server 2008.

One Main Thing to Remember here is I have taken LINQ-To-SQL with DataContextClass Name as : DataClasses1DataContext

The Table Named: Math

The Complete Definition of Math Table looks like this:

Linq1.png


Code Toolbox Requirements:

  • 5 Labels, 6 Buttons, 3 Textboxes , 1 Grid View

The Complete Code of WebForm1.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Simple_LINQ_Application.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">
    <title></title
>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <center>
            <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999"
                BorderStyle="Solid" BorderWidth="3px" AutoGenerateColumns="false" CellPadding="4"
                CellSpacing="2" ForeColor="Black" OnLoad="PageLoad">
                <FooterStyle BackColor="#CCCCCC" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                <RowStyle BackColor="White" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#808080" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#383838" />
                <Columns>
                    <asp:BoundField HeaderText="Id" DataField="Id" ReadOnly="true" SortExpression="true" />
                    <asp:BoundField HeaderText="Type of Operation" DataField="Type_of_Operation" />
                    <asp:BoundField HeaderText="First Number" DataField="Number1" />
                    <asp:BoundField HeaderText="Second Number" DataField="Number2" />
                    <asp:BoundField HeaderText="Result" DataField="Result" />
                </Columns>
            </asp:GridView>
        </center>
        <center>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Text="Please Specify Type of Operation" Font-Bold="true"
                            Font-Italic="true"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Please Enter First Number" Font-Bold="true"
                            Font-Italic="true"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Please Enter Second Number" Font-Bold="true"
                            Font-Italic="true"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Click Here for Addition Results" OnClick="Button1Click"
                            Width="215px" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button5" runat="server" Text="Click Here for Multiplication Results"
                            OnClick="Button5Click" Width="216px" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button2" runat="server" Text="Minimum Values" OnClick="Button2Click"
                            Width="214px" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button3" runat="server" Text="Average of Results" Width="218px" OnClick="Button3Click" />
                    </td>
                </tr>
                <tr
>

                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button4" runat="server" Text="Complete Total of Results" Width="219px"
                            OnClick="Button4Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td
>

                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button6" runat="server" Text="Take Operation of Query" Width="210px"
                            OnClick="Button6Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" ForeColor="Red" Font-Bold="true" Font-Italic="true"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label5" runat="server" ForeColor="Red" Font-Bold="true" Font-Italic="true"
                            Font-Size="Larger"></asp:Label>
                    </td>
                </tr>
            </table>
        </center>
    </div>
    </form
>
</body>
</
html>

Data Retrieval Logic:

var data = new DataClasses1DataContext();
var simpleretrieval = (from abc in data.Maths orderby ID select abc);

Data Insertion Logic with Addition:

var data = new DataClasses1DataContext();
var insertdata = new Math();
insertdata.Type_of_Operation = TextBox1.Text;
insertdata.Number1 = Convert.ToInt32(TextBox2.Text);
insertdata.Number2 = Convert.ToInt32(TextBox3.Text);
insertdata.Result = Convert.ToInt32(TextBox2.Text) + Convert.ToInt32(TextBox3.Text);
data.Maths.InsertOnSubmit(insertdata);
data.SubmitChanges();


Finding the Min Number Logic:

int[] number ={
Convert.ToInt32(TextBox2.Text),
Convert.ToInt32(TextBox3.Text)
};
int number1 = number.Min();


Finding the Average of Results Logic:

var data = new DataClasses1DataContext();
var simpleAverage = (from abc in data.Maths select abc).Average(d => d.Result);


Finding the Total of Results Logic:

var data = new DataClasses1DataContext();
var simpleTotal = (from abc in data.Maths select abc).Sum(d => d.Result);


The Complete Code of WebForm1.aspx.cs looks like this:

using System;
using System.Linq;

namespace Simple_LINQ_Application
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void PageLoad(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            var data = new DataClasses1DataContext();
            var simpleretrieval = (from abc in data.Maths orderby ID select abc);
            GridView1.DataSource = simpleretrieval;
            GridView1.DataBind();
        }

        protected void Button1Click(object sender, EventArgs e)
        {
            var data = new DataClasses1DataContext();
            var insertdata = new Math();
            insertdata.Type_of_Operation = TextBox1.Text;
            insertdata.Number1 = Convert.ToInt32(TextBox2.Text);
            insertdata.Number2 = Convert.ToInt32(TextBox3.Text);
            insertdata.Result = Convert.ToInt32(TextBox2.Text) + Convert.ToInt32(TextBox3.Text);
            data.Maths.InsertOnSubmit(insertdata);
            data.SubmitChanges();
            Bind();
            Label1.Text = "<center><b>Records Inserted</b><center>";
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
        }

        protected void Button2Click(object sender, EventArgs e)
        {
            int[] number ={
Convert.ToInt32(TextBox2.Text),
Convert.ToInt32(TextBox3.Text)
};
            int number1 = number.Min();

            Label5.Text = (string.Format("<center><b><i>The Minimum number is :{0}</i></b></center>", number1));

        }

        protected void Button3Click(object sender, EventArgs e)
        {
            var data = new DataClasses1DataContext();

            var simpleAverage = (from abc in data.Maths select abc).Average(d => d.Result);

            Label5.Text = (string.Format("<center><b><i>The Average number is :{0}</i></b></center>", simpleAverage));

        }

        protected void Button4Click(object sender, EventArgs e)
        {
            var data = new DataClasses1DataContext();

            var simpleTotal = (from abc in data.Maths select abc).Sum(d => d.Result);

            Label5.Text = (string.Format("<center><b><i>The Complete Total of Results is :{0}</i></b></center>", simpleTotal)); 
 
        }
 
        protected void Button5Click(object sender, EventArgs e)
        {
            var data = new DataClasses1DataContext();
            var insertdata = new Math();
            insertdata.Type_of_Operation = TextBox1.Text;
            insertdata.Number1 = Convert.ToInt32(TextBox2.Text);
            insertdata.Number2 = Convert.ToInt32(TextBox3.Text);
            insertdata.Result = Convert.ToInt32(TextBox2.Text) * Convert.ToInt32(TextBox3.Text);
            data.Maths.InsertOnSubmit(insertdata);
            data.SubmitChanges();
            Bind();
            Label1.Text = "<center><b>Records Inserted</b><center>";
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
        }

        protected void Button6Click(object sender, EventArgs e)
        {
            var data = new DataClasses1DataContext();
            var takeResults = (from r in data.Maths orderby ID select r).Skip(1).Take(1);
            GridView1.DataSource = takeResults;
            GridView1.DataBind();
        }
 
    }
}


The Output of the Application looks like this:

Linq2.png

Output for Data Insertion Input with Addition Operation:

I have take 23 as my first number and 23 as my second number: So the total is 46.

Linq3.png

Output for Finding Minimum Number:

I have taken two numbers here 6 and 7 and the minimum value shown here is 6.

Linq4.png

Output for Finding Average of Results:

Linq5.png

Output for Total of Results:

Linq6.png

Output using Take and Skip Methods:

Linq7.png


I hope this article is useful for you.