LINQ: Get Table Column Names

Viewed 47313

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

11 Answers

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

Related