lundi 30 avril 2018

2 similar foreach statements, 1 works other doesnt

i have a txt file that contains user info that is read to a list, i then have a foreach statements the first simply prints containtes of a list. i want the second foreach to check the contents of the list to a user input then use, and then only use the info from the list that matches the user input. my second foreach statement only seems to check the first variable within the list. how do i go about fixing this?

var fileLines = File.ReadAllLines("stylist.txt").Where(line => !string.IsNullOrEmpty(line)).ToList();

var stylists = new List<Stylist>();


for (int i = 0; i + 4 < fileLines.Count; i += 5)
{
    stylists.Add(new Stylist
    {
        FirstName = fileLines[i],
        LastName = fileLines[i + 1],
        Email = fileLines[i + 2],
        Phone = fileLines[i + 3],
        Rate = fileLines[i + 4]
     });
}


foreach (var i in stylists) // prints the stylists first and last names
{
    Console.WriteLine(i.FirstName + " " + i.LastName);
}

string stylistSelected = Console.ReadLine(); // user input


 //for (int z = 0; z < stylists.Count; z++)
 foreach (var z in stylists)
 {

     if (z.FirstName == stylistSelected)

i am also using a custom class, the code for this is below

public class Stylist // creates the stylist class and variables needed for each stylist
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Rate { get; set; }
}

my txt file looks like so

john
smith
js@123.456
123456789
123

jane
doe
jd@123.456
987654321
1456

bryan
smith
bs@123.987
147258369
321

Aucun commentaire:

Enregistrer un commentaire