Click here to Skip to main content
13,508,005 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 12 Sep 2013

Dynamically Build LINQ to SQL Classes at Runtime

, 13 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Dynamically build Linq2Sql classes based off SQL table


The attached .cs file looks at a SQL table, generates a LINQ to SQL Object at runtime that can be used to insert, update, and query the database. It is useful for working with your own interactive C# interpreter. The problem it solves; you don't need to design an EDMX file and compile before using.

Using the Code

//make an IDbConnection
IDbConnection oconn = new System.Data.SqlClient.SqlConnection(
  @"Data Source=;Initial Catalog=db;user id=usr;password=pwd;");

//pass the connection and the table you want
object o = DynamicL2S.L2SClassObject(oconn, "table");
Type itemType = o.GetType();

//create an instance and insert into db
using (DataContext dc = new DataContext(oconn))
    var item = (dynamic)Activator.CreateInstance(itemType, new Object[] { });

    item.oid = Guid.NewGuid();
    item.x = 9;
    item.y = 9;
    item.z = 9;
    item.color = Color.Blue.ToArgb();



//retrieve all items
using (DataContext dc = new DataContext(oconn))
    var res = from r in dc.GetTable(itemType).Cast<dynamic>()
              select r;

    dataGridView1.DataSource = res.ToList();

//get properties
System.Reflection.PropertyInfo[] method = o.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo oInf in method)
    ret += oInf.Name + " - " + oInf.PropertyType.Name + "\n";

Points of Interest

It uses the C# compiler to compile C# classes at run time. It uses object extension methods to force LINQ to allow use of dynamic type when casting.

Credit to Sources


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


About the Author

Software Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questionthe generated sql query with out where statement Pin
BakriB12-Jun-15 17:46
memberBakriB12-Jun-15 17:46 
QuestionNice Pin
Chona11716-Feb-15 2:22
memberChona11716-Feb-15 2:22 
QuestionMore examples & minor problems Pin
Dewey12-Sep-13 22:59
memberDewey12-Sep-13 22:59 
AnswerRe: More examples & minor problems Pin
oryan13-Sep-13 10:31
professionaloryan13-Sep-13 10:31 
GeneralRe: More examples & minor problems Pin
Dewey24-Sep-13 14:12
memberDewey24-Sep-13 14:12 

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
Web02 | 2.8.180417.1 | Last Updated 13 Sep 2013
Article Copyright 2013 by oryan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid