.net LINQ 到数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10382343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ to DataTable
提问by Wild Goat
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.
我有大 DataTable 我想得到这个 DataTable 的子集,表示为 DataTable。简要说明如何选择 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;
Maybe my code is wrong, how do I get "managers" and "Phone" columns from one DataTable to another without looping thought it?
也许我的代码是错误的,如何在不循环的情况下将“经理”和“电话”列从一个数据表获取到另一个数据表?
采纳答案by Kris Krause
Reference the DataTableExtensions -
参考 DataTableExtensions -
http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx
http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx
Then...
然后...
var whatever = dTable.AsEnumerable();
Then per the MSDN example...
然后根据 MSDN 示例...
var productNames = from products in table.AsEnumerable()
select products.Field<string>("ProductName");
Edit/update: Unfortunately I do not think there is a built in direct cast back to a DataTable with a different schema. You haveto 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.
编辑/更新:不幸的是,我认为没有内置的直接转换回具有不同架构的 DataTable。你必须使用数据表吗?我相信它......因为重构和测试你的代码可能需要太多的努力。祝你好运,让我们随时关注,因为使用强类型列表更有趣。
回答by Matt
You could write:
你可以写:
var query = from row in dTable.AsEnumerable()
select new
{
manager = row.Field<string>("Manager"),
phone = row.Field<string>("Phone")
};
回答by Jayesh Sorathia
You can execute LINQ query on DataTable Or Specific table on DataSet with the help of the AsEnumerable.
您可以在 AsEnumerable 的帮助下对 DataTable 或 DataSet 上的特定表执行 LINQ 查询。
Here is example this might be helpful.
这是示例,这可能会有所帮助。
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
ds.DataSetName = "products";
dt.TableName = "product";
dc = new DataColumn("product_id",long.MaxValue.GetType());
dt.Columns.Add(dc);
dc = new DataColumn("product_name");
dt.Columns.Add(dc);
dr = dt.NewRow();
dr["product_id"] = 1;
dr["product_name"] = "Monitor";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["product_id"] = 2;
dr["product_name"] = "Mouse";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["product_id"] = 3;
dr["product_name"] = "KeyBoard";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["product_id"] = 4;
dr["product_name"] = "LCD";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
IEnumerable<DataRow> objResult1 = from tbl in dt.AsEnumerable()
where tbl.Field<long>(0) <= 2
select tbl;
Response.Write("<b>Query Results 1</b>");
foreach (DataRow row in objResult1)
{
Response.Write(string.Format("<br/>Product ID: {0} , Product Name: {1}", row.Field<long>(0), row.Field<string>(1)));
}
IEnumerable<DataRow> objResult2 = from tbl in ds.Tables[0].AsEnumerable()
let product_name = tbl.Field<string>(1)
where product_name.StartsWith("Key")
|| product_name.StartsWith("Mo")
select tbl;
Response.Write("<br/><br/><b>Query Results 2</b>");
foreach (DataRow row in objResult2)
{
Response.Write(string.Format("<br/>Product ID: {0} , Product Name: {1}", row.Field<long>(0), row.Field<string>(1)));
}

