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)



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 )

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

Create a website or blog at