Convert to IDataReader from List

Anwind

Well-known member
Joined
Nov 6, 2021
Messages
48
Programming Experience
1-3
Hello

Convert to Idatareader from List ??

Is it possible??

Main CS:
using System.Data.Linq
.
.
public class Main
{
    Reader csvdata = new Reader(new StreamReader(D:\\CSVFILE.CSV), true);
    IDataReader iDataReader = csvdata;
 
    IEnumerable<IDataReader> ret = IDataReaderEx.AsEnumerable(); // I need to return an IDataReader in the end.
}


The source below is a static type. (using system.collections.generic)

Is the grammar correct???


IDataEx:
using System.Data.Linq
using System.Collections.generic

public static class IDataReaderEx
{ // The source below is a static type. (using system.collections.generic)
    public static class CsvList
    {
        public static string Name;
        public static string Id;
    }

    public static IEnumerable<IDataReader> AsEnumerable(this System.Data.IDataReader source)
    {
        List<CsvList> list = new List<CsvList>();
        CsvList clsList = new CsvList(); 

        while (source.Read())
        {
            clsList.Name = source["Name"].ToString();
            clsList.Id = source["Id"].ToString();   

            list.Add(clsList);
        }
      
        var retList = list.where(n=>n.Name == "ABC");

        yield return ???? ;
    }
}
 
Last edited:
As I've said in your other threads, you'll need to write a class that implements the IDataReader interface. With your implementation you can have anything as its data source. You can use a list, an array, a data reader, a serial port, etc. Yes, it's possible.
 
As I've said in your other threads, you'll need to write a class that implements the IDataReader interface. With your implementation you can have anything as its data source. You can use a list, an array, a data reader, a serial port, etc. Yes, it's possible.
Please Example Source..
Please ...
 
There are examples out there already. Didn't Skydiver already provide one? What exactly do you not understand? The point of an example is that it demonstrates a principle that you can then apply to other similar situations. It sounds like you're not actually asking for an example but rather for someone to write code for this specific scenario so that you can just copy and paste it. That's not how this site works. We'll help you but that implies that you're doing your part. I'm not seeing that here.
 
There are examples out there already. Didn't Skydiver already provide one? What exactly do you not understand? The point of an example is that it demonstrates a principle that you can then apply to other similar situations. It sounds like you're not actually asking for an example but rather for someone to write code for this specific scenario so that you can just copy and paste it. That's not how this site works. We'll help you but that implies that you're doing your part. I'm not seeing that here.

Again, I wrote the question in detail.
I am eagerly awaiting your teaching.
Please have mercy
 
Last edited:
As I've said in your other threads, you'll need to write a class that implements the IDataReader interface. With your implementation you can have anything as its data source. You can use a list, an array, a data reader, a serial port, etc. Yes, it's possible.
Again, above I wrote the question in detail.
I am eagerly awaiting your teaching.
Please have mercy..Please My Teacher
 
Last edited:
What did we just say about deleting posts? Post #3 has been to restored.
 
Also what did way about going back and back editing posts? Your original post did not have that code originally. This is not StackOverflow where you keep massaging your original question. If you have updates to refine your question, post within the same thread with your updates. Don't edit the original post or previous post that people have already responded to.
 
Also what did way about going back and back editing posts? Your original post did not have that code originally. This is not StackOverflow where you keep massaging your original question. If you have updates to refine your question, post within the same thread with your updates. Don't edit the original post or previous post that people have already responded to.
I am Sorry. please answer my question. please teach me.
I am eagerly awaiting your teaching.
 
You did not restate the question. In your post #3 you were begging for sample source code. You were not begging for source code in your post #1. Since that at the time you had post #1, I replied in post #2, and then you replied in post #3, what question were you restating?
 
That is incorrect.

You need to do the following pseudo-code:
C#:
class DataReaderOnList<T> : IDataReader
    where T : IDataRecord
{
    IList<T> _list = null;
    int _index = -1;

    public DataReaderOnList(IList<T> list)
    {
        _list = list;
    }

    // Implement IDataReader by iterating through the list

    public bool Read()
    {
        _index++;
        return _list != null && index < _list.Count;       
    }

    public void Close() => _list = null;

    :

    // Implement IDataRecord by calling the IDataRecords in the list

    public object this[string name] => _list[_index][name];
    public object this[int index] => _list[_index][index];

    public string GetString(int index) => _list[_index].GetString(index);


    :
}

class CsvRow : IDataRecord
{
    object [] _fields = null;

    public CsvRow(object [] fields)
    {
        _fields = fields;
    }

    // Implement IDataRecord by accessing the array of fields

    public object this[string name] => _fields[name];
    public object this[int index] => _fields[index];

    public string GetString(int index) => _fields[index].ToString();

    :
}

var records = new List<CsvRecord>();
var reader = new CsvReader(...);
while (reader.Read())
{
    var fields = new object[reader.FieldCount];
    reader.GetValues(fields);
    records.Add(new CsvRow(fields));
}

var filteredAndSortedList = records.Where(r => r["Name"] == "ABC").OrderBy(r => r["Id"]).ToList();

var listAsDataReader = new DataReaderOnList(filteredAndSortedList);
while (listAsDataReader.Read())
{
    Console.WriteLine("The value of column 9 is {0}", listAsDataReader[9]);
    Console.WriteLine("The value of the 'Id' column is {0}", listAsDataReader["Id"]);
}
 
And I'll let you know that code above is going to break one of your other requirements that you stated in past, but then likely later edited or deleted in your multiple post edits and deletes. The code above is going to end up loading the entire CSV into memory by the time lines 54-61 are finished executing.
 
The code I pseudo-code from post #14 was meant to demonstrate the concepts of how to go from a IDataReader (the CsvReader), to an IList, to filtering and sorting into another IList, and finally back into an IDataReader. It was not meant to be just copied and pasted in.
 
So, there's this nuget library called Sylvan.Data.Csv which comes with CsvDataReader, which inherits DbDataReader, which implements IDataReader..

(and ifyou don't use the netstandard flavor of lumenworks, that also already implements IDataReader)

To want an IDataReader, and use a device that implements IDataReader, and read all data into a list, and then implement something on top of the list so you can have something to return that implements IDataReader is rather a nonsense. Just return the original IDataReader your CSV reading library already implements..

..but I get the feeling I'm banging my head on a brick wall!
 
Last edited:
Back
Top Bottom