ARTICLE

Listing columns name with type of a table using LINQ

Posted by Dhananjay Kumar Articles | LINQ December 29, 2010
After my last article, I got a question asking how to list all the Column names of a given table using LINQ.
Reader Level:


After my last article, 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 that 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 the table exists in DataContext or not

get1.gif

Dbo.Person is the name of the table.

We are looking through all the tables and finding whether a given table exists or not. Once we find the table we need to list all the column names.

get2.gif

Full source code is as below. In the code below we are determining whether the dbo.Person table exists or not. If it exists in DataContext then list all the column names.

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)
                    {
                        Console.WriteLine(r1.MappedName);
                    }
                }
                
            }
            Console.ReadKey(true);
        }

    }

}

Output

get3.gif

Login to add your contents and source code to this article
post comment
     

Hi......I need help in getting a list of all column names in a combo box.... when the table name is given as a parameter in the method/function in svc.cs file(service file).... Kindly help...

Posted by Riddhi Valecha Jan 13, 2013

Hi... Nice work....It really helped me out a lot... Just one query - I want all the table names and column names(of a selected table) in combo box in Silverlight application. Backend is LINQ-to-SQL. What to write in place of "Console.WriteLine()" ?? Please Help!!

Posted by Riddhi Valecha Nov 19, 2012

Thanks for the help. I am using Entity Framework and when I introduce the LINQ to SQL I start running into problems. I'll have to find another way to do this.

Posted by Andrew Fannin Jun 29, 2011

Hi Andrew, 1. Right click on Project and add a new item 2. From Data tab choose LINQ to SQL Class 3. Then choose Data Source 4. The class created by LINQ to SQL is DataContext class. I would be happy to see ur error if u can share Other way to find is open abc.dbml.cs then search for string by pressing Ctrl+F Datacontext then see the class name would be abcDatacontext:Datacontext then use abcdatacontext I hope it helps

Posted by Dhananjay Kumar Jun 29, 2011

Maybe I'm missing something obvious here, but what exactly is DataClasses1DataContext()? I have tried creating a new DataContext but always seem to get errors.

Posted by Andrew Fannin Jun 28, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.