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

LINQ FAQ: Part 2

, 28 Jul 2009
This FAQ article is dedicated to LINQ to SQL.
CRUD.zip
LINQCrudWithEntities
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
obj
Debug
TempPE
Properties
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
obj
Debug
TempPE
Properties
LINQtoSQLStoredProc.zip
LINQtoSQLStoredProc
LINQtoSQLStoredProc
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQtoSQLStoredProc.dll
LINQtoSQLStoredProc.csproj.user
obj
Debug
TempPE
Properties
OneMany.zip
LINQtoSQL
SimpleLinqExample
App_Data
Sales.mdf
Sales_log.LDF
bin
SimpleLinqExample.dll
obj
Debug
TempPE
Properties
SimpleLinqExample.csproj.user
Optimizing_LINQ.zip
SimpleLinqExample.dll
SimpleLinqExample.csproj.user
SQL
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=localhost;Initial Catalog=Sales;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>");
                }
            }
        }

       
    }
}

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)

Share

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
Web03 | 2.8.140921.1 | Last Updated 29 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid