Click here to Skip to main content
13,453,725 members (44,049 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 16 May 2011

Linq: Relationship Code Sample

, 16 May 2011
Rate this:
Please Sign up or sign in to vote.
How to Implement Relationship in LINQ using C# and ASP.NET for Beginners


This sample shows how to implement Relationship Using LINQ in C# and ASP.NET for a beginner.

How To Do

  1. Create an ASP.NET Web application using C# in Visual Studio 2008.

    File-> New -> Project -> ASP.NET Web Application

  2. Add a database file to app_data - "student.mdf".
    1. Right Click App_Data, Add->New Item->Data->SQL Server DataBase.
    2. Give name Student.mdf.

    3. Go to the Server Explore, Expand Student DataBase, In Tables, Add New Table
    4. Add two tables, Student and Mark.

      Table: StudentColumns: StudentID int, Name nvarchar(50), Age int

      Table: Mark
      Columns: StudentID int, Class nvarchar(50), Mark int

      Add some sample data for the Student and Mark tables, (student id should be same in Mark and Student table)

      For that, right click the table and click show table data, you can add data here.

  3. LINQ to SQL

    Right click the solution, add New Item->Data-> Linq to SQL Class.

  4. Give Name "LinqToStudents.dbml".

  5. Pick SQL Server explore, drag the two tables to the window:

    Save the Project.

  6. Drag a DataGrid view to the Default.aspx Page:

  7. Use the below code snippets pageload of the project:
    LinqToStudentsDataContext linqStud = new LinqToStudentsDataContext();
    var query = from s in linqStud.Students
    join h in linqStud.Marks on s.StudentID equals h.StudentID 
    select new {h.StudentID ,s.Name, };      
    GridView1.DataSource = query; 

Run the project by pressing F5.


  • Release 1


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


About the Author

sabindas k s
Software Developer Banque Saudi Fransi
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
After205020-May-11 2:55
memberAfter205020-May-11 2:55 
GeneralRe: My vote of 2 Pin
ak9937218-Sep-12 9:46
memberak9937218-Sep-12 9:46 
GeneralNice article for begining LinQ Pin
Johnson.Sebastian/353771918-May-11 21:42
memberJohnson.Sebastian/353771918-May-11 21:42 
GeneralRealy useful article for linq developer Pin
pcm_it16-May-11 21:18
memberpcm_it16-May-11 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180318.3 | Last Updated 16 May 2011
Article Copyright 2011 by sabindas k s
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid