ettocs
ettocs

Reputation: 21

C# Read from CSV database where specific number appears

I've got a CSV that is setup somewhat like a database.

Data is as follows

ID, firstName,lastName etc

I need to be able to start reading from a user entered id number, record that data set and then store it as strings.

I may be going about this completely the wrong way, very new to coding. Any input is appreciated!

string sid = txtsid.Text; //< This is the user entered data

        string filePath = Server.MapPath("~/App_Data/employees.csv");
        string[] empData = System.IO.File.ReadAllLines(filePath); //*READ Employee CSV AND STORE *//
        var emps = from line in empData
                   let data = line.Split('|')
                   where line.id == sid //< this doesn't work
                   select new { id = data[0], dob = data[1], firstName = data[2], lastName = data[3], gender = data[4], joindate = data[5], };


        foreach (var s in emps)

        {
            lblResult.Text = s.id;
            Console.WriteLine("[{0}] {1} {2}", s.id, s.firstName, s.lastName);

        }

Upvotes: 1

Views: 59

Answers (1)

BugFinder
BugFinder

Reputation: 17868

where line.id == sid //< this doesn't work

No, it wont

why?

line is just 1 string, string does not have a property id.

data is the split up string, so your test probably is on that, and it would be data[0] as your split items wont have names.

Id imagine however that line being

 where data[0] == sid 

probably will work

Upvotes: 2

Related Questions