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:
I have separated it into three cs files.(Main.cs, DataReaderOnList.CS, CsvRow.CS)
Does the
class DataReaderOnList<T> , class CsvRow, class Main
you taught me have to divide the CS file into three ?
You don't have to put them in separate files right now. You will likely have to eventually to satisfy a discerning code reviewer. But to satisfy the compiler, it could careless if you have separate files or a single file.

Again, you are being distracted by trivia instead of getting to the core of the matter. Just the like with the overall all goal of what you are trying to do. The core of the matter is getting your CSV data sequentially in a particular order after it has been filtered, sorted, and a row number determined for that row being examined. You got sucked into needing to dump SQLite for OLEDB, and now getting sucked into absolutely needing the data accessible as a IDataReader. Do you really need it be in an IDataReader?

So far you've been drawing us into an X-Y Problem.

@cjard hit the nail on the head in one of your other threads, you need to step back and tell us what you are trying to do and what the final form that you need the data to be for your algorithm that needs that CSV data. What is that problem X that you are trying to solve?
 
Last edited:
Regarding those compiler errors. It's pseudo-code. As I previous said, it's not meant to just be pasted in. It's meant for you to read and and understand the ideas.

 
Your Answer) The core of the matter is getting your CSV data sequentially in a particular order after it has been filtered, sorted, and a row number determined for that row being examined.
==> Yes
If that is truly the core of the matter, then it shouldn't matter to you whether the data was taken from the CSV and then imported into SQLite. Nor it should it matter if you are reading using the CSV using Sylvan CvsReader, and then you do some filtering and sorting using LINQ, and then end up with an IList instead of an IDataReader.
 
If that is truly the core of the matter, then it shouldn't matter to you whether the data was taken from the CSV and then imported into SQLite. Nor it should it matter if you are reading using the CSV using Sylvan CvsReader, and then you do some filtering and sorting using LINQ, and then end up with an IList instead of an IDataReader.
Please teach me. Sylvan C# Discusses board... please

How do you do what you said
 
Last edited:
The pseudo code in post #14 already showed you how to do this. Just get rid of the stuff that uses the IDataReader and focus on the reading, filtering and sorting. Here's new pseudo code. It is lines 54-70 without needing to deal with converting back to an IDataReader and just handling the data with a bonus of also giving you the row number.

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

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

int rowNum = 0;
foreach (var row in filteredAndSortedList)
{
    ++rowNum;
    Console.WriteLine($"Processing row {rowNum}:");
    Console.WriteLine("The value of column 9 is {0}", row[9]);
    Console.WriteLine("The value of the 'Id' column is {0}", row["Id"]);
}
 
My Teacher, Click web site !!
Converting List To IDataReader – Andrey Zavadskiy – SQL and .NET developer, trainer, speaker

Questions 1 ) Is your code adaptable as shown on the site here?
No because I was deliberately trying to keep things simple and fast without having you go down another rabbit hole of using reflection. Furthermore, that code will require you to map all your fields read from the CSV into a class, only to turn around and have that code there use reflection to read it from each of the fields you had mapped.

The above was assuming that you what you mean with the word "adaptable" was that it could easily take any List<T> without restrict to the type of T. My pseudo code in post #14 wants.T to be a IDataRecord. Also the pseudo code was written to demonstrate splitting the responsibility of implementing the data reader separate for the data record. You were utter baffled by my code in your other thread where I was mixing the two together. My pseudo code was meant for you to think and use your brain to understand the problem.

Now, if what you mean by "adaptable" is that you can just copy and paste the code without having to use your brain and learn then there you have it.

I think you need to distinguish between your requests for "teach me" vs. "gimme the codez".
 
No because I was deliberately trying to keep things simple and fast without having you go down another rabbit hole of using reflection. Furthermore, that code will require you to map all your fields read from the CSV into a class, only to turn around and have that code there use reflection to read it from each of the fields you had mapped.

The above was assuming that you what you mean with the word "adaptable" was that it could easily take any List<T> without restrict to the type of T. My pseudo code in post #14 wants.T to be a IDataRecord. Also the pseudo code was written to demonstrate splitting the responsibility of implementing the data reader separate for the data record. You were utter baffled by my code in your other thread where I was mixing the two together. My pseudo code was meant for you to think and use your brain to understand the problem.

Now, if what you mean by "adaptable" is that you can just copy and paste the code without having to use your brain and learn then there you have it.

I think you need to distinguish between your requests for "teach me" vs. "gimme the codez".
please teach me.. please
 
Questions 2 ) How do I implement ListDataReader<Client>(clients) in the code below?


C#:
using (IDataReader reader = new ListDataReader<Client>(clients))
{
while (reader.Read())
{
...
}
}
You would implement it just like that.

Somewhere before that code you would have the something like this pseudo code:
C#:
class Client
{
    public string Name { get; set; }
    public int Id { get; set; }
}

:

var clients = new List<Client>();
var reader = new CsvReader(...);
while (reader.Read())
{
    var client = new Client();
    client.Name = reader["Name"];
    client.Id = reader["Id"];
    clients.Add(client);
}

Notice how that pseudo code looks just like lines 56-61 of post #14 and lines 1-8 of post #26.

If you were Sylvan as you are claiming to be using in one your posts that you deleted. The you can let Sylvan do the mapping of the fields into the Client class. In fact, you could just get a the List<Client> from Sylvan by letting it do the mapping and just calling ToList(). But alas, in post #25 in this thread you say that you are all into Lumenworks.
 
Questions 3 )
q1.png

Error CS0535 IDataReader implement interface member <== Need to create a new CS file? If you need to create a new CS file, what should be the contents?
No you don't need to create a new CS file. You simply need to implement the interface by writing out all the required methods and properties for that interface. Visual Studio even makes it easier for you. If you hover over the error, and press the lightbulb icon with the error overlay, it'll give you an option to implement the interface.

1670505778197.png


I highly recommend picking the second option: "Implement interface with Dispose pattern".
 
I can't solve this problem without you.
Yes, you can if you step back, think, analyze, and try to understand. You are currently in "let me search the Internet to find code that I can just copy-and-paste" to drop into my existing code. I refer you back to post #20.
 
==> please write the code.. please...
I get paid to write code. I for outside gigs, I charge $120 an hour.

But this forum is a learning forum. I volunteer my time freely to try to teach. Not spoonfeed.
 
I don't know how to use the code of post14, post27.
Because it's pseudo code. You are supposed to use it as a diagram or map towards understanding. It's not meant to be copied and pasted into your IDE.
 
(I'm quoting the above because you seem to love to delete posts and edit posts.)

But by post #21 you said that you don't really need an IDataReader:
Your Answer) The core of the matter is getting your CSV data sequentially in a particular order after it has been filtered, sorted, and a row number determined for that row being examined.
==> Yes

Then I responded in post #24:
Your Answer) The core of the matter is getting your CSV data sequentially in a particular order after it has been filtered, sorted, and a row number determined for that row being examined.
==> Yes
If that is truly the core of the matter, then it shouldn't matter to you whether the data was taken from the CSV and then imported into SQLite. Nor it should it matter if you are reading using the CSV using Sylvan CvsReader, and then you do some filtering and sorting using LINQ, and then end up with an IList instead of an IDataReader.

and you responded in post #25:
If that is truly the core of the matter, then it shouldn't matter to you whether the data was taken from the CSV and then imported into SQLite. Nor it should it matter if you are reading using the CSV using Sylvan CvsReader, and then you do some filtering and sorting using LINQ, and then end up with an IList instead of an IDataReader.
Please teach me. Sylvan C# Discusses board... please

How do you do what you said

And now you are back to wanting an IDataReader in post #36.
I need to return an iDataReader.
what should i do...

I give up. You are just going around in circles. Good luck with your project.
 
Back
Top Bottom