是否有基于 DbDataReader 的 IQueryable 实现? [英] Is there an implementation of IQueryable over DbDataReader?

查看:20
本文介绍了是否有基于 DbDataReader 的 IQueryable 实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多使用原始 ADO.NET(DbConnection、DbDataReader 等)的现有代码.我想过渡到对新代码使用 LINQ to SQL,但现在将现有代码和新代码都放在一组统一的存储库类后面.

I have a lot of existing code which uses raw ADO.NET (DbConnection, DbDataReader, etc). I would like to transition to using LINQ to SQL for new code, but for now put both the existing and new code behind a unified set of Repository classes.

我遇到的一个问题是:我希望存储库类将结果集公开为 IQueryable<>,这是我通过 LINQ to SQL 免费获得的.如何将现有的 DbDataReader 结果集包装在 IQueryable 中?我是否必须从头开始在 DbDataReader 上实现 IQueryable?

One issue I have is this: I would like the Repository classes to expose result sets as IQueryable<> which I get for free with LINQ to SQL. How do I wrap my existing DbDataReader result sets in an IQueryable? Do I have to implement IQueryable over DbDataReader from scratch?

注意我知道 LINQ to DataSet,但由于内存规模问题,我不使用 DataSet,因为我处理的结果集可能非常大(大约 1000 秒).这意味着 IQueryable over DbDataReader 实现也需要高效(即不要将结果缓存在内存中).

Note I am aware of LINQ to DataSet, but I don't use DataSets because of memory scale issues, as the result sets I deal with can be quite large (order of 1000s). This implies that the IQueryable over DbDataReader implementation will need to be efficient as well (i.e. don't cache results in memory).

推荐答案

我看不到实现 IQueryable<T> 的任何好处 - 这表明比实际可用的功能更多 - 但是,你可以很容易地将它实现为 IEnumerable<T> ,但需要注意的是它是一次性的.迭代器块将是一个合理的选择:

I can't see any benefit in implement IQueryable<T> - that suggests more functionality than is actually available - however, you could implement it as an IEnumerable<T> easily enough, with the caveat that it is once-only. An iterator block would be a reasonable choice:

    public static IEnumerable<IDataRecord> AsEnumerable(
        this IDataReader reader)
    {
        while (reader.Read())
        {
            yield return reader; // a bit dangerous
        }
    }

有点危险"是因为调用者可以将其退回并滥用它......

The "a bit dangerous" is because the caller could cast it back and abuse it...

这篇关于是否有基于 DbDataReader 的 IQueryable 实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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