如何使 DataTable 可枚举?

发布于 2024-07-25 05:29:09 字数 878 浏览 7 评论 0原文

我无法在 DataTable 上使用 AsEnumerable(),我使用的是 C# 3,但我只是针对 2.0 框架(LINQ 功能由 LINQBridge)。 有什么方法可以在不使用 Select() 的情况下使 DataTable 可枚举吗?

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 不可用

顺便说一句,我尝试了这个: http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/01/15/284.aspx,但有编译错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灰色世界里的红玫瑰 2024-08-01 05:29:09
    public static IEnumerable<DataRow> EnumerateRows(this DataTable table)
    {
        foreach (var row in table.Rows)
        {
            yield return row;
        }
    }

允许您拨打:

bool isExisting = (bdsAttachments.DataSource as DataTable).EnumerateRows().Any(dr => (string)dr["filename"] == filename);
    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);
梦中的蝴蝶 2024-08-01 05:29:09
  1. IEnumerable; rows = dataTable.AsEnumerable(); (System.Data.DataSetExtensions.dll)
  2. IEnumerable; rows = dataTable.Rows.OfType(); (System.Core.dll)
  1. IEnumerable<DataRow> rows = dataTable.AsEnumerable(); (System.Data.DataSetExtensions.dll)
  2. IEnumerable<DataRow> rows = dataTable.Rows.OfType<DataRow>(); (System.Core.dll)
煮茶煮酒煮时光 2024-08-01 05:29:09

严格保持枚举器为 2.0:

public static IEnumerable<DataRow> getRows(DataTable table)
{
    foreach (DataRow row in table.Rows)
    {
        yield return row;
    }
}

然后使用 linqbridge 进行调用,如下所示:

bool isExisting = getRows(bdsAttachments.DataSource as DataTable).Any(row => (string)row["filename"] == filename);

Keeping your enumerator strictly 2.0:

public static IEnumerable<DataRow> getRows(DataTable table)
{
    foreach (DataRow row in table.Rows)
    {
        yield return row;
    }
}

Then call with linqbridge like this:

bool isExisting = getRows(bdsAttachments.DataSource as DataTable).Any(row => (string)row["filename"] == filename);
一身骄傲 2024-08-01 05:29:09

您可以尝试将 DataTable 转换为 IEnumerable 并迭代

//your data exists as a DataTable
DataTable dt = (DataTable)bdsAttachments.DataSource;
foreach (DataRow row in dt)
{
    if (row["filename"] == filename)
        return row;
}

该集合:foreach 将迭代列表并搜索文件名(我假设您正在搜索具有该文件名的第一个 DataRow,而不是与文件名匹配的所有行)。

You can try casting the DataTable as IEnumerable and iterate over the set:

//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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文