如何使数据表枚举? [英] How can I make DataTable enumerable?
问题描述
bool isExisting =(bdsAttachments.DataSource as DataTable)。 Select()。Any(xxx =>(string)dr [filename] == filename);
更新:
使它看起来像这样:
bool isExisting =(bdsAttachments.DataSource as DataTable).AsEnumerable()。Any(xxx => ;(string)dr [filename] == filename);
我正在看到DataTable的Select方法返回一个副本,我在想只需使用AsEnumerable,问题是我只是针对2.0框架,System.Data.DataSetExtensions不可用
BTW,我尝试过: http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/ 01/15 / 284.aspx ,但有编译错误。
public static IEnumerable< DataRow> EnumerateRows(这个DataTable表)
{
foreach(table.Rows中的var row)
{
yield return row;
}
}
允许您拨打:
bool isExisting =(bdsAttachments.DataSource as DataTable).EnumerateRows()。any(dr =>(string)dr [filename] ==文件名);
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() ?
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
BTW, i tried this: http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/01/15/284.aspx, but has compilation errors.
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);
这篇关于如何使数据表枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!