Click here to Skip to main content
12,501,833 members (50,521 online)
Click here to Skip to main content

Stats

29.4K views
1.5K downloads
76 bookmarked
Posted

LINQ FAQ: Part 3

, 27 Jul 2009 CPOL
Concurrency handling, compiled queries, CRUD implementation, and mapping simple .NET classes with XML file configuration.
LINQConcurrency
LINQConcurrency
App_Data
bin
LINQConcurrency.dll
LINQConcurrency.csproj.user
Properties
LINQCrudWithEntities
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
Properties
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
Properties
LINQPerformance
Send
Book1.xlsx
LINQtoSQL
SimpleLinqExample
App_Data
bin
SimpleLinqExample.dll
obj
Debug
TempPE
Properties
SimpleLinqExample.csproj.user
SQL
SimpleLINQExample
SimpleLINQExample
WindowFormLINQ
Country.mdf
Country_log.LDF
WindowFormLINQ
bin
Debug
WindowFormLINQ.exe
WindowFormLINQ.vshost.exe
WindowFormLINQ.vshost.exe.manifest
Release
WindowFormLINQ.exe
obj
Debug
Release
Properties
WindowFormLINQ.csproj.user
WebAppMappingXML
WebAppMappingXML
App_Data
bin
WebAppMappingXML.dll
obj
Debug
TempPE
Properties
WebAppMappingXML.csproj.user
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;
using System.Data.Linq;
namespace SimpleLinqExample
{
    
    // This is a simple class with LINQ mappings
    [Table(Name = "Customer")]
    public class clsCustomerEntity
    {
        [Column(DbType= "nvarchar(50)")]
        public string CustomerCode;

        [Column(DbType = "nvarchar(50)")]
        public string CustomerName;
    }

    // This is a simple LINQ class with encapsulated properties
    [Table(Name = "Customer")]
    public class clsCustomerEntityWithProperties
    {
        private int _CustomerId;
        private string _CustomerCode;
        private string _CustomerName;
       
        [Column(DbType = "nvarchar(50)")]
        public string CustomerCode
        {
            set
            {
                _CustomerCode = value;
            }
            get
            {
                return _CustomerCode;
            }
        }

        [Column(DbType = "nvarchar(50)")]
        public string CustomerName
        {
            set
            {
                _CustomerName = value;
            }
            get
            {
                return _CustomerName;
            }
        }

        [Column(DbType = "int", IsPrimaryKey = true)]
        public int CustomerId
        {
            set
            {
                _CustomerId = value;
            }
            get
            {
                return _CustomerId;
            }
        }
    }

    // This is a simple class which shows customer and addresses
    // relationship using entityset
    [Table(Name = "Customer")]
    public class clsCustomerWithAddresses
    {
        private int _CustomerId;
        private string _CustomerCode;
        private string _CustomerName;
        private EntitySet<clsAddresses> _CustomerAddresses;

        [Column(DbType="int",IsPrimaryKey=true)]
        public int CustomerId
        {
            set
            {
                _CustomerId = value;
            }
            get
            {
                return _CustomerId;
            }
        }
        
        [Column(DbType = "nvarchar(50)")]
        public string CustomerCode
        {
            set
            {
                _CustomerCode = value;
            }
            get
            {
                return _CustomerCode;
            }
        }
        
        [Column(DbType = "nvarchar(50)")]
        public string CustomerName
        {
            set
            {
                _CustomerName = value;
            }
            get
            {
                return _CustomerName;
            }
        }

        [Association(Storage = "_CustomerAddresses",ThisKey="CustomerId", OtherKey = "CustomerId")]
        public EntitySet<clsAddresses> Addresses
        {
            set
            {
                _CustomerAddresses = value;
            }
            get
            {
                return _CustomerAddresses;
            }

        }
    }
    [Table(Name = "CustomerAddresses")]
    public class clsAddresses
    {
        private int _Customerid;
        private int _AddressId;
        private string _Address1;
        private EntityRef<clsPhone> _Phone;
        [Column(DbType="int")]
        public int CustomerId
        {
            set
            {
                _Customerid = value;
            }
            get
            {
                return _Customerid;
            }
        }
        [Column(DbType = "int", IsPrimaryKey = true)]
        public int AddressId
        {
            set
            {
                _AddressId = value;
            }
            get
            {
                return _AddressId;
            }
        }
        [Column(DbType = "nvarchar(50)")]
        public string Address1
        {
            set
            {
                _Address1 = value;
            }
            get
            {
                return _Address1;
            }
        }
        [Association(Storage = "_Phone", 
         ThisKey = "AddressId", OtherKey = "AddressId")]
        public clsPhone Phone
        {
            set
            {
                _Phone.Entity = value;
            }
            get
            {
                return _Phone.Entity;
            }
        }
    }
    [Table(Name = "Phone")]
    public class clsPhone
    {
        private int _PhoneId;
        private int _AddressId;
        private string _MobilePhone;
        private string _LandLine;
        
        [Column(DbType = "int", IsPrimaryKey = true)]
        public int PhoneId
        {
            set
            {
                _PhoneId = value;
            }
            get
            {
                return _PhoneId;
            }
        }
        [Column(DbType = "int")]
        public int AddressId
        {
            set
            {
                _PhoneId = value;
            }
            get
            {
                return _PhoneId;
            }
        }
        [Column(DbType = "nvarchar")]
        public string MobilePhone
        {
            set
            {
                _MobilePhone = value;
            }
            get
            {
                return _MobilePhone;
            }
        }
        [Column(DbType = "nvarchar")]
        public string LandLine
        {
            set
            {
                _LandLine = value;
            }
            get
            {
                return _LandLine;
            }
        }
    }

   
}

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...

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