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

One-Many and One-One relationships using LINQ to SQL

, 5 Jul 2009
One-many and one-one relationships using LINQ to SQL.
LINQtoSQL.zip
LINQtoSQL
SimpleLinqExample
App_Data
Sales.mdf
Sales_log.LDF
bin
SimpleLinqExample.dll
obj
Debug
TempPE
Properties
SimpleLinqExample.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=.\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>");
                }
            }
        }

       
    }
}

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