【问题标题】:How to get the Columns from a WebMatrix Query?如何从 WebMatrix 查询中获取列?
【发布时间】:2015-10-15 04:09:46
【问题描述】:

标题非常不言自明,看起来应该是一件容易的事,但我尝试过的一切都没有奏效:

这是我的代码,工作正常,但表格是可变的,所以我需要知道它返回的列:

var data = db.Query("SELECT * FROM " + Table);

以下是我尝试过的技术列表:

data.GetType().GetProperties().ToList(); 
// prints 'Int32 Count' and 'System.Object Item [Int32]'

data.GetDynamicMemberNames()
// Error: '...IEnumerable<dynamic> does not have a definition for GetDynamicMemberNames'
// I also tried using System.Linq and System.Dynamic

I could also iterate through a loop, but there's got to be a more elegant way, right?

我想以列名的List&lt;String&gt; 结尾。

【问题讨论】:

    标签: c# webmatrix


    【解决方案1】:

    List&lt;String&gt; cols = data.First().Columns;


    原来Columns Propery 是IList&lt;string&gt; 类型。

    但是,它是数据结果的单个行的属性(行是 DynamicRecord 数据类型),因此很遗憾无法从结果对象中访问它(在我的示例中为data)。要访问它,您需要获取一行,.First() 似乎是一种非常简单的方法。

    这是我的完整代码,以防有人需要:

    WebMatrix.Data.Database db = new WebMatrix.Data.Database();
    db.Open("ConnectionString");
    var data = db.Query("SELECT * FROM " + Table);
    List<String> cols = data.First().Columns;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-24
      • 2017-08-17
      • 2013-08-20
      • 2014-10-26
      • 2019-05-28
      • 2019-03-07
      • 2011-07-26
      • 2014-11-16
      相关资源
      最近更新 更多