Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading from a csv file, and removing any rows that only have ""

Reading from a csv file using stream reader and then splitting each value from the row that’s being read by a comma.

I would like to remove any row that has empty values within the whole row.

{"test", "test", "test", "test"}
{"test1", "test1", "test1", "test1"}
{"", "", "", ""}

I would like my method to not process that third row if its empty.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using (StreamReader reader = new StreamReader(stream))
            {
                reader.ReadLine();

                while (reader.Peek() != -1)
                {

                    var line = reader.ReadLine();

                    string[] data = line.Split(',');

                    // Convert data into an object
                    ...
                }
            }

I would only like to convert the data into an object if it doesn’t look like the 3rd array. {"", "", "", ""}

>Solution :

You can use Linq

// At least 1 empty value
bool hasEmpty = data.Any(string.IsNullOrWhiteSpace);

// All values are empty
bool allEmpty = data.All(string.IsNullOrWhiteSpace);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading