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 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.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
Get All the Tables Name using LINQ
Deferred Execution and Immediate Execution in LINQ
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...
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!!
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.
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
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.