有IQueryable的超过DbDataReader实现? [英] Is there an implementation of IQueryable over DbDataReader?

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

问题描述

我有很多现有的code,它使用原始的ADO.NET(的DbConnection,DbDataReader,等)。我想过渡到使用LINQ to SQL进行新的code,但现在把两个现有的和新的code背后一套统一的信息库类。

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?我必须从头开始实施的IQueryable超过DbDataReader?

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到数据集,但我不使用,因为内存的尺度问题数据集,因为我处理的结果集是非常大的(为了1000的)。这意味着的IQueryable超过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&LT; T&GT; - 这意味着更多的功能比实际可用的 - 但是,你可以实现它作为一个的IEnumerable&LT; T&GT; 很轻松了,需要提醒的是它是一次性的。迭代器块将是一个合理的选择:

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

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

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