Listing columns name with type of a table using LINQ

After my last post, I got a question asking how to list all the Column names of a given table using LINQ.

Before going through this post, I recommend you to read Get All the Tables Name using LINQ

Let us say, you have a table called dbo.Person and you need to find all the columns of this particular table. 
So first we need to find whether table exists in DataContext or not 


Dbo.Person is name of the table.
We are looking through all the tables and finding where given table exists or not. Once we get table we need to list the entire columns name

Full source code is as below. In below code we are finding whether dbo.Person table is existing or not. If existing in DataContext then listing all the columns name


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

namespace ConsoleApplication2
    class Program
        static void Main(string[] args)

            DataClasses1DataContext context = new DataClasses1DataContext();
            var datamodel = context.Mapping;

            foreach (var r in datamodel.GetTables())
                if (r.TableName.Equals("dbo.Person", StringComparison.InvariantCultureIgnoreCase))

                    foreach (var r1 in r.RowType.DataMembers)



Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s