LINQ到数据表 [英] LINQ to DataTable

查看:120
本文介绍了LINQ到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很大的数据表我想要得到psented的数据表或者该数据表重新$ P $的子集。 简要说我怎么在数据表中选择特定列。

I have big DataTable I want to get subset of this DataTable represented as DataTable either. Briefly saying how do I select particular columns in DataTable.

我是想这样的事情,但它不工作...

I was trying something like this but it doesn't work...

DataTable dTable = new DataTable();
...
...
...
        DataTable dt = from field in dTable
                       where field.Field<string>("Manager")
                       where field.Field<string>("Phone")
                       select field;

也许我的code是错的,我怎么得到经理人和手机列从一个数据表到另一个没有循环想到呢?

Maybe my code is wrong, how do I get "managers" and "Phone" columns from one DataTable to another without looping thought it?

推荐答案

引用的DataTableExtensions -

Reference the DataTableExtensions -

http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx

然后...

var whatever = dTable.AsEnumerable();

然后按MSDN的例子...

Then per the MSDN example...

var productNames = from products in table.AsEnumerable() 
      select products.Field<string>("ProductName");

编辑/更新:不幸的是,我不认为这是一个建在直接投回到以不同架构的DataTable。您有无使用数据表?我相信......因为它可能是太多精力重构和测试code。祝你好运,让我们作为使用强类型的列表的工作都更加的乐趣的。

Edit/update: Unfortunately I do not think there is a built in direct cast back to a DataTable with a different schema. You have to use a DataTable? I believe it... as it might be too much effort to refactor and test your code. Good luck and keep us posted as working with strongly-typed lists are much more fun.

这篇关于LINQ到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆