开发者

Using Linq with a DbDataReader

I am trying to make use of the code in this question to implement a query like this:

    public void LoadLive(DbConnection pConnection)
    {
        using (DbDataReader lReader = pConnection.ExecuteReader("..."))
        {
            mList.AddRange(from t in lReader select new MyObject { Name = t.GetString(0) });
        }
    }

When I attempt to compile this (with the extension method in place), I receive this error:

 error CS1934: Could not find an implementation of the query pattern for source type 'System.Data.Common.DbDataReader'.  'Select' not found.  Consider explicitly specifying the type of the ra开发者_Python百科nge variable 't'.

Am I missing something about how this is supposed to work?


You must call the extension method from the answer in the linked question:

 mList.AddRange(from t in lReader.AsEnumerable() 
                select new MyObject { Name = t.GetString(0) });


Unless you are going to write your own extension method, Select, Where, etc all return an IEnumerable and the typical method signature will resemble Func<DbDataReader, myT>. What you are looking for is something like Jon Skeet's sample here.


Compiler error CS1934 is produced when no standard query operators are implemented for a given datasource.

In your case (DbDataReader), you could specify the type of t as IDataRecord:

mList.AddRange(from IDataRecord t in lReader
               select new MyObject { Name = t.GetString(0) });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜