Using LINQ, how can I get the column names of a table? C# 3.0, 3.5 framework
-
Retagged to `linq-to-sql`, please edit your question if you meant some other kind of "LINQ". – Alex Bagnolini Nov 26 '09 at 12:09
-
Unfortunately this query run inside LINQPad so how do I get the context in LINQPad? – Lennie De Villiers Dec 19 '09 at 19:37
11 Answers
Maybe It is too late but, I solved this problem by this code
var db = new DataContex();
var columnNames = db.Mapping.MappingSource
.GetModel(typeof(DataContex))
.GetMetaType(typeof(_tablename))
.DataMembers;
-
1Thanks, you just saved me a boat load of time. This is better than the accepted answer because this shows how to use the DataContext.Mapping property. – Doctor Jones Jan 30 '13 at 18:09
-
3For VB.Net use `GetType(DataContext)` instead of `typeof(DataContext)`. – Zarepheth Jun 15 '13 at 22:12
The below code will worked from returns all the column names of the table
var columnnames = from t in typeof(table_name).GetProperties() select t.Name

- 24,660
- 56
- 77
- 117

- 159
- 1
- 3
-
2Not sure why this was not upvoted, it is the most elegant answer. Here is the lambda syntax equivalent: var columnnames = typeof(table_name).GetProperties().Select(t => t.Name); – draconis Jan 28 '13 at 13:40
-
3Maybe this wasn't upvoted as you don't only get the columns but additional properties as well. It's usually a partial class with extensibility in mind after all. – mbx Feb 03 '14 at 08:00
I stumbled upon this question looking for the same thing and didn't see a really good answer here. This is what I came up with. Just throw it into LINQPad under C# expression mode.
from t in typeof(UserQuery).GetProperties()
where t.Name == "Customers"
from c in t.GetValue(this,null).GetType().GetGenericArguments()[0].GetFields()
select c.Name
Modify as you see fit.

- 8,741
- 3
- 33
- 38
I assume you mean by using LINQ to SQL, in which case look at the DataContext.Mapping property. That's what I use.
If you don't mean that, perhaps you can elaborate on what you are trying to achieve?

- 28,883
- 9
- 61
- 81
-
I want to write a program using LINQPad (C#) That can build SQL insert statements off course for this I require the column names of the tables. – Lennie De Villiers Nov 26 '09 at 12:01
-
6could this be clarified a bit...this is not much of an answer. – Morgan Herlocker Feb 25 '11 at 16:02
-
1Some example usage would be great. The Mapping API is a bit un-intuitive to say the least... – Doctor Jones Jan 30 '13 at 18:09
Use the ExecuteQuery method on your data context and execute this SQL script:
var columnNames = ctx.ExecuteQuery<string>
("SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('your table name');");
This gives you an IEnumerable<string>
with all the column names in that table you specified.
Of course, if you want and need to, you could always retrieve more information (e.g. data type, max length) from the sys.columns
catalog view in SQL Server.

- 732,580
- 175
- 1,330
- 1,459
sp_help 'TableName'
An option for a LinqPad SQL window to ms sql server

- 6,696
- 3
- 46
- 36
In LinqPad:
TableNames.Take(1) works.
It is fast to type. (Although you in an ideal world, it would be nicer to not select any rows.)
Note it is the pluralized form of TableName. You can also do Take(0) and look at the SQL results tab.

- 6,696
- 3
- 46
- 36
I am using .net core 3.0 and none of these solutions worked for me. My solution was a brute forced approach using Newtonsoft Json serializer.
var dt = this._context.Prodmats.First(); // get the first row of my table
var json = JsonConvert.SerializeObject(dt);
var tva2 = new string(json.Where(c => char.IsLetter(c) || char.IsDigit(c) || c == ':'
|| c == ',' || char.IsWhiteSpace(c) ).ToArray()) ;
var arr = tva2.Split(',');
var col = new List<string>();
foreach (var val in arr)
{
var ch = val.Split(':');
var trimCh = ch[0];
col.Add(trimCh.Trim());
}
// col <= has the columns list

- 1,011
- 8
- 5
-
This gives property names, not table column names. Also, the question is not about EF, already has many answers, and there are similar questions that are about EF. – Gert Arnold Jan 02 '21 at 15:17