Best method to use IDataReader as IEnumerable?

后端 未结 6 846
攒了一身酷
攒了一身酷 2021-02-06 13:16

I need to use Linq on any IDataReader implementations like this

var c = sqlDataReader.AsEnumerable().Count();

Example:

<
6条回答
  •  无人共我
    2021-02-06 13:54

    I've used the following but I like @Serge's suggestion better and it reminds be of what I believe I used to do but then forgot that IDataReader implements

    var reader = command.ExecuteReader();
    var records = Enumerable
        .Range(0, int.MaxValue)
        .TakeWhile(i => reader.Read())
        .Select(i => reader as IDataRecord);
    

提交回复
热议问题