我怎样才能让数据表枚举? [英] How can I make DataTable enumerable?

查看:224
本文介绍了我怎样才能让数据表枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能使用AsEnumerable()在数据表中,我使用C#3,但我只是针对2.0框架(LINQ能力礼貌的 LINQBridge )。有没有什么办法可以让数据表枚举,而无需使用选择()?

 布尔isExisting =(bdsAttachments.DataSource如数据表)。选择()任何(XXX =>(串)博士[文件名] ==文件名);
 

更新:

我想它,使它看起来像这样:

 布尔isExisting =(bdsAttachments.DataSource如数据表).AsEnumerable()任何(XXX =>(串)博士[文件名] ==文件名);
 

我收到一个暗示说DataTable的选择方法返回一个副本,我想只使用AsEnumerable,问题是,我只是针对2.0框架,System.Data.DataSetExtensions不可用。

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

解决方案

 公共静态的IEnumerable< D​​ataRow的> EnumerateRows(此数据表表)
    {
        的foreach(在table.Rows VAR行)
        {
            得到的回报排;
        }
    }
 

允许您拨打:

 布尔isExisting =(bdsAttachments.DataSource如数据表).EnumerateRows()的任何。(DR =>(串)博士[文件名] ==文件名);
 

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屋!

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