获取“列标题”来自 EnumerableRowCollectionLINQ 结果
希望这相对简单。在对 DataTable
执行 LINQ
查询后,我想将结果放入计划动态创建的另一个 DataTable
中。我需要的是在 LINQ
查询结果集合中查找列标题
的名称。
编辑:我基本上试图返回 LINQ 查询
var results= from a in dt.AsEnumerable()
where a.Field<int>("ID") == i
select new
{
ID = a.Field<int>("ID"),
a = a.Field<double>("A"),
b = a.Field<double>("B")
};
- “ID”、“a”和“b”。
真的希望这是有道理的!
Hopefully this is relatively simple. After I perform a LINQ
query on a DataTable
I want to put the results in a another DataTable
which I plan to create dynamically. What I need is to find the name of th column titles
in the LINQ
Query reults collection.
EDIT: The LINQ Query
var results= from a in dt.AsEnumerable()
where a.Field<int>("ID") == i
select new
{
ID = a.Field<int>("ID"),
a = a.Field<double>("A"),
b = a.Field<double>("B")
};
I am basically trying to return - "ID", "a", and "b".
Really hope that makes sense!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于查询结果集是一组匿名类型项,因此您不能对 DataTable 和 DataColumn/Rows 执行任何操作...但是您可以使用反射检索名称,因为每列将表示为匿名类型的公共属性:
这将返回:
Since query results set is a set of anonymous type items you can NOT do anything in terms of DataTable and DataColumn/Rows... But you can retrieve names using reflection since each column will be represented as public property of the anonymous type:
This would return:
作为快速整理(可能不是最好的,但这是匆忙完成的 - 非常匆忙!):
或者,如果您已经有一个 linq 查询,请将其转换回数据表,然后获取列:
希望这是有道理的
As a quick knock-up (probably not the best but this was done in a hurry - a big hurry!!):
Or if you already have a linq query in place, convert it back to a data table then get the columns:
Hope this makes sense