We need to list the names of all the tables of the DataContext. This is very simple using LINQ mapping. 1. Add the namespace 2. Get the model of the datacontext type. DataClasses1DataContext is your datacontext class created by LINQ to SQL. 3. Use GetTables() method to list all the table 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) { var datamodel = new AttributeMappingSource().GetModel(typeof(DataClasses1DataContext)); foreach (var r in datamodel.GetTables()) { Console.WriteLine(r.TableName); } Console.ReadKey(true); } }}Output There is one more scenario. If you already have an instance of a DataContext and you want to list all the table names then you need to get the model as below: Here context is instance of datacontext class. 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()){Console.WriteLine(r.TableName);}Console.ReadKey(true);}}}Output
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: