gpt4 book ai didi

c# - 如何从 WebMatrix 查询中获取列?

转载 作者:行者123 更新时间:2023-11-30 17:42:48 29 4
gpt4 key购买 nike

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

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

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<String> 结尾列名称。

最佳答案

List<String> cols = data.First().Columns;


事实证明 Columns Propery是一个 IList<string>类型。

但是,它是数据结果的单个行的属性(一行是 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;

关于c# - 如何从 WebMatrix 查询中获取列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31611329/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com