LINQ: Get Table Column Names

后端 未结 11 626
-上瘾入骨i
-上瘾入骨i 2020-12-31 01:14

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

相关标签:
11条回答
  • 2020-12-31 01:38
                var query = from x in DataBase.Table_Name
                            select x.Column_Name;
    
    0 讨论(0)
  • 2020-12-31 01:44

    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.

    0 讨论(0)
  • 2020-12-31 01:48

    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;
    
    0 讨论(0)
  • 2020-12-31 01:50

    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
    
    0 讨论(0)
  • 2020-12-31 01:53

    sp_help 'TableName'

    An option for a LinqPad SQL window to ms sql server

    0 讨论(0)
提交回复
热议问题