Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / WPF

A LINQ Tutorial: Mapping Tables to Objects

Rate me:
Please Sign up or sign in to vote.
4.89/5 (119 votes)
12 Dec 2009CPOL17 min read 533.1K   14.4K   321  
A beginner's LINQ tutorial that walks you through mapping your SQL Server database tables and relationships to objects, and how to retrieve that data via simple LINQ queries.
/*********************************************************************
 * A LINQ Tutorial: Mapping Tables to Objects
 * By: Abby Fichtner, http://www.TheHackerChickBlog.com
 * Article URL: http://www.codeproject.com/KB/linq/linqtutorial.aspx
 * Licensed under The Code Project Open License (CPOL)
 *********************************************************************/

using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace LINQDemo
{
    #pragma warning disable 0169        // disable never used warnings for fields that are being used by LINQ

    [Table( Name = "BookCategories" )] 
    public class Category : IBookCollection
    {
        [Column( IsPrimaryKey = true, IsDbGenerated = true )] public int Id { get; set; }
        [Column] public string Name { get; set; }

        private EntitySet<Book> _books = new EntitySet<Book>();
        [Association( Name = "FK_Books_BookCategories", Storage = "_books", OtherKey = "categoryId", ThisKey = "Id" )]
        public ICollection<Book> Books {
            get { return _books; }
            set { _books.Assign( value ); }
        }
    }
}

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)


Written By
Software Developer Microsoft
United States United States
Abby Fichtner is a Microsoft Developer Evangelist and author of The Hacker Chick Blog.

She's been developing custom software applications, wearing every hat imaginable, since 1994. Although, technically, she got her start at the age of 8 when her father brought home an Atari 800. In the evenings, they would sit together and type in the machine code from the Atari magazines – because that was the way serious geeks got their computer games!

Today, she works for Microsoft as a Developer Evangelist to the startup community - helping them to create the next generation of software.

Comments and Discussions