将列表数据与readline输入进行比较

时间:2018-04-26 22:32:51

标签: c# list for-loop if-statement foreach

我正在读取一个数组的txt文件,然后从数组中获取数据并将其放入自定义类型列表中。

function f03, c1, s1, n1
  ; a bunch of code goes here
  return, ch
end

自定义类代码是

var StylistFile = File.ReadLines("stylist.txt").ToArray();
var lineCount = File.ReadLines("stylist.txt").Count();

int k = (lineCount) / 6; // used tocalculates number of stylists by looking at the number of lines
int L = 1;

List <Stylist> stylists = new List<Stylist>();


for (L = 1;L <=k; L++) 
{

    string[] stylistsL = new string[6];
    Stylist stylistL = new Stylist();
    stylists.Add(stylistL);

    foreach (var i in stylists) 
    {
        stylistL.FirstName = StylistFile[((L - 1) * 6) + 0];
        stylistL.LastName = StylistFile[((L - 1) * 6) + 1];
        stylistL.Email = StylistFile[((L - 1) * 6) + 2];
        stylistL.Phone = StylistFile[((L - 1) * 6) + 3];
        stylistL.Rate = StylistFile[((L - 1) * 6) + 4];

        stylistsL[0] = stylistL.FirstName;
        stylistsL[1] = stylistL.LastName;
        stylistsL[2] = stylistL.Email;
        stylistsL[3] = stylistL.Phone;
        stylistsL[4] = stylistL.Rate;
        stylistsL[5] = "";
    }
}
foreach (var i in stylists) // prints the stylists first and last names
{
    Console.WriteLine(i.FirstName + " " + i.LastName);
}

string stylistSelected = Console.ReadLine();

//foreach (var z in stylists) // goes through each stylist in list
for (int z = 0; z <= stylists.Count; z++)
{

    if ((stylistSelected == stylists[z].FirstName) || (stylistSelected == stylists[z].FirstName + " " + stylists[z].LastName)) // checks to see if stylistSelected match current stylists
    {
       Console.WriteLine(stylists[z].FirstName + " " + stylists[z].LastName);

txt文件格式如此

public class 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; }
}

我能够获取代码来打印列表中的每个项目,但我找不到一种方法来获取代码来检查列表中的数据与readline字符串如何进行此操作?< / p>

我尝试过同时使用john smith js@123.456 123456789 123 jane doe jd@123.456 987654321 1456 foreach循环。我已经尝试为forfor循环使用新的int变量

第二个问题是,我是否可以foreachstylistLstylist1,...等等,以便与txtx文件中的数据进行比例缩放?

1 个答案:

答案 0 :(得分:0)

要从文本文件中填充List<Stylist>,我们可以执行以下操作,我们将文件读入数组(忽略任何空行),然后设置循环计数器以增加{{1每次迭代(因为每5行代表一个Stylist),在循环内部我们设置新5的属性并将其添加到我们的Stylist

下面的代码假定每5行代表一个造型师(它忽略文件中的任何空白行):

List<Stylist>