Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

LINQ FAQ: Part 3

, 27 Jul 2009
Concurrency handling, compiled queries, CRUD implementation, and mapping simple .NET classes with XML file configuration.
LINQConcurrency.zip
LINQConcurrency
LINQConcurrency
App_Data
bin
LINQConcurrency.dll
LINQConcurrency.csproj.user
Properties
LINQCrudWithEntities.zip
LINQCrudWithEntities
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
Properties
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
Properties
LINQPerformance.zip
LINQPerformance
Send
Book1.xlsx
LINQtoSQL
SimpleLinqExample
App_Data
bin
SimpleLinqExample.dll
obj
Debug
TempPE
Properties
SimpleLinqExample.csproj.user
SQL
SimpleLINQExample
SimpleLINQExample
WindowFormLINQ
Country.mdf
Country_log.LDF
WindowFormLINQ
bin
Debug
WindowFormLINQ.exe
WindowFormLINQ.vshost.exe
WindowFormLINQ.vshost.exe.manifest
Release
WindowFormLINQ.exe
obj
Debug
Release
Properties
Settings.settings
WindowFormLINQ.csproj.user
WebAppMappingXML.zip
WebAppMappingXML
WebAppMappingXML
App_Data
bin
WebAppMappingXML.dll
obj
Debug
TempPE
Properties
WebAppMappingXML.csproj.user
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
namespace SimpleLinqExample
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnectionString = @"Data Source=mum-9018;Initial Catalog=TstServer;Integrated Security=True";
        //string strConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Sales.mdf;Integrated Security=True;User Instance=True";
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected void cmdSimpleLINQtoSQL_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);
            Table<clsCustomerEntity> objTable = objContext.GetTable<clsCustomerEntity>();

            foreach (clsCustomerEntity objCustomer in objTable)
            {
                Response.Write(objCustomer.CustomerName + "<br>");
            }
        }

        protected void SimpleLINQtoSQlWithQuery_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);
            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerEntityWithProperties>()
                          where objCustomer.CustomerCode == txtCustomerCode.Text
                          select objCustomer;
            foreach (clsCustomerEntityWithProperties objCustomer in MyQuery)
            {
                Response.Write(objCustomer.CustomerName + "<br>");
            }
        }

        protected void SimpleLINQtoSQlWithSetGet_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);
            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerEntityWithProperties>()
                          select objCustomer;
            foreach (clsCustomerEntityWithProperties objCustomer in MyQuery)
            {
                Response.Write(objCustomer.CustomerName + "<br>");
            }
        }

        protected void LINQtoSQLrelationShip_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);

            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerWithAddresses>()
                          select objCustomer;
                   
            foreach (clsCustomerWithAddresses objCustomer in MyQuery)
            {
               
                Response.Write(objCustomer.CustomerName + "<br>");
                
                foreach (clsAddresses objAddress in objCustomer.Addresses)
                {
                    Response.Write("===Address:- " + objAddress.Address1 + "<br>");
                    Response.Write("========Mobile:- " + objAddress.Phone.MobilePhone + "<br>");
                    Response.Write("========LandLine:- " + objAddress.Phone.LandLine + "<br>");
                }
            }

           
        }

        protected void LINQtoSQLusingDataLoadOption_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);
            DataLoadOptions objDataLoadOption = new DataLoadOptions();
            objDataLoadOption.LoadWith<clsCustomerWithAddresses>(clsCustomerWithAddresses => clsCustomerWithAddresses.Addresses);
            objDataLoadOption.LoadWith<clsAddresses>(clsAddresses => clsAddresses.Phone);
            objContext.LoadOptions = objDataLoadOption;
            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerWithAddresses>()
                          select objCustomer;

            foreach (clsCustomerWithAddresses objCustomer in MyQuery)
            {

                Response.Write(objCustomer.CustomerName + "<br>");

                foreach (clsAddresses objAddress in objCustomer.Addresses)
                {
                    Response.Write("===Address:- " + objAddress.Address1 + "<br>");
                    Response.Write("========Mobile:- " + objAddress.Phone.MobilePhone + "<br>");
                    Response.Write("========LandLine:- " + objAddress.Phone.LandLine + "<br>");
                }
            }
        }

        protected void LINQQueryComparisonUsingCompiledQueries_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Stopwatch objStopWatch = new System.Diagnostics.Stopwatch();
            
            DataContext objContext = new DataContext(strConnectionString);
            objStopWatch.Start();
            IQueryable<clsCustomerEntity> objCustomers = clsCompiledQuery.getCustomers(objContext, txtCustomerCode.Text);
            foreach (clsCustomerEntity objCustomer in objCustomers)
            {
                Response.Write(objCustomer.CustomerName + "<br>");
            }
            
            objStopWatch.Stop();
            Response.Write("The time taken to execute query with compilation is : " + objStopWatch.ElapsedMilliseconds.ToString() + " MillionSeconds<br>");
            objStopWatch.Reset();
            objStopWatch.Start();
            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerEntity>()
                          where objCustomer.CustomerCode == txtCustomerCode.Text
                          select objCustomer;
            foreach (clsCustomerEntity objCustomer in MyQuery)
            {
                Response.Write(objCustomer.CustomerName + "<br>");
            }
            objStopWatch.Stop();
            Response.Write("The time taken to execute query with  out compilation is : " + objStopWatch.ElapsedMilliseconds.ToString() + " MillionSeconds");
        }

       
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 28 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid