Linq: Get a list of all tables within DataContext

Sergey picture Sergey · Apr 10, 2009 · Viewed 12.5k times · Source

I have a DataContext (Linq to Sql) with over 100 tables, is it possible to get a list of all those tables and lets say print them to the console? This might be a silly question.

Thanks.

Answer

Jacob Proffitt picture Jacob Proffitt · Apr 10, 2009

It's much easier than above and no reflection required. Linq to SQL has a Mapping property that you can use to get an enumeration of all the tables.

context.Mapping.GetTables();