Lennie De Villiers
Lennie De Villiers

Reputation: 5319

LINQ: Get Table Column Names

Using LINQ, how can I get the column names of a table? C# 3.0, 3.5 framework

Upvotes: 21

Views: 51293

Answers (11)

Julian Shaw
Julian Shaw

Reputation: 1081

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

Upvotes: -1

Curtis Yallop
Curtis Yallop

Reputation: 7309

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.

Upvotes: 0

Curtis Yallop
Curtis Yallop

Reputation: 7309

sp_help 'TableName'

An option for a LinqPad SQL window to ms sql server

Upvotes: 0

William
William

Reputation: 539

            var query = from x in DataBase.Table_Name
                        select x.Column_Name;

Upvotes: 0

ravi kumar
ravi kumar

Reputation: 159

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

Upvotes: 15

Don
Don

Reputation: 507

I used this code in LinqPad

from t in typeof(table_name).GetFields() select t.Name

Upvotes: 2

erkan
erkan

Reputation: 231

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;

Upvotes: 23

Marty Neal
Marty Neal

Reputation: 9523

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.

Upvotes: 7

marc_s
marc_s

Reputation: 754258

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.

Upvotes: 2

RichardOD
RichardOD

Reputation: 29157

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?

Upvotes: 6

Matt Kocaj
Matt Kocaj

Reputation: 11535

Iterate the properties of your L2S classes with reflection

Upvotes: 0

Related Questions