Click here to Skip to main content
12,455,701 members (84,895 online)
Click here to Skip to main content

Stats

82.4K views
1.2K downloads
51 bookmarked
Posted

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

, 5 Jul 2009 CPOL
One-many and one-one relationships using LINQ to SQL.
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


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 6 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid