C# 如何使 DataTable 可枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1041955/
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
How can I make DataTable enumerable?
提问by Hao
I cannot use AsEnumerable() on DataTable, I'm using C# 3 but I'm just targeting 2.0 framework (LINQ capability is courtesy of LINQBridge). Is there any way I can make DataTable enumerable without using Select() ?
我不能在 DataTable 上使用 AsEnumerable(),我使用的是 C# 3,但我只是针对 2.0 框架(LINQ 功能由LINQBridge 提供)。有什么方法可以使 DataTable 可枚举而不使用 Select() 吗?
bool isExisting = (bdsAttachments.DataSource as DataTable).Select().Any(xxx => (string)dr["filename"] == filename);
Update:
更新:
I wanted it to make it look like this:
我想让它看起来像这样:
bool isExisting = (bdsAttachments.DataSource as DataTable).AsEnumerable().Any(xxx => (string)dr["filename"] == filename);
I'm getting an inkling that the Select method of DataTable returns a copy, I'm thinking to just use AsEnumerable, the problem is I'm just targeting 2.0 framework, System.Data.DataSetExtensions is not available
我得到一个暗示 DataTable 的 Select 方法返回一个副本,我想只使用 AsEnumerable,问题是我只是针对 2.0 框架,System.Data.DataSetExtensions 不可用
BTW, i tried this: http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/01/15/284.aspx, but has compilation errors.
顺便说一句,我试过这个:http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/01/15/284.aspx,但有编译错误。
采纳答案by Timothy Carter
public static IEnumerable<DataRow> EnumerateRows(this DataTable table)
{
foreach (var row in table.Rows)
{
yield return row;
}
}
Allows you to call:
允许您调用:
bool isExisting = (bdsAttachments.DataSource as DataTable).EnumerateRows().Any(dr => (string)dr["filename"] == filename);
回答by Chris Thompson
You can try casting the DataTable as IEnumerable and iterate over the set:
您可以尝试将 DataTable 转换为 IEnumerable 并迭代该集合:
//your data exists as a DataTable
DataTable dt = (DataTable)bdsAttachments.DataSource;
foreach (DataRow row in dt)
{
if (row["filename"] == filename)
return row;
}
The foreach will iterate through the list and search of the filename (I assume you're searching for the first DataRow with that filename, not all rows that match the filename).
foreach 将遍历列表并搜索文件名(我假设您正在搜索具有该文件名的第一个 DataRow,而不是与文件名匹配的所有行)。
回答by Joel Coehoorn
Keeping your enumerator strictly 2.0:
严格保持您的枚举器 2.0:
public static IEnumerable<DataRow> getRows(DataTable table)
{
foreach (DataRow row in table.Rows)
{
yield return row;
}
}
Then call with linqbridge like this:
然后像这样用 linqbridge 调用:
bool isExisting = getRows(bdsAttachments.DataSource as DataTable).Any(row => (string)row["filename"] == filename);
回答by abatishchev
IEnumerable<DataRow> rows = dataTable.AsEnumerable();
(System.Data.DataSetExtensions.dll)IEnumerable<DataRow> rows = dataTable.Rows.OfType<DataRow>();
(System.Core.dll)
IEnumerable<DataRow> rows = dataTable.AsEnumerable();
(System.Data.DataSetExtensions.dll)IEnumerable<DataRow> rows = dataTable.Rows.OfType<DataRow>();
(System.Core.dll)