-1

我有一个写给这个类的列表:

  public class keyfrs
{
     public keyfrs() { }
     public long regID { get; set; }
     public long ID { get; set; }
     public string county { get; set; }
     public string state { get; set; }
}
List<keyfrs> k = {1,2,3,4}] regID
                 {A,B,C,D}  ID

我有另一个这样的列表类:

 public class states
{
   public states() { }
   public long regID { get; set; }
   public string state { get; set; }
   public string county { get; set; }
}
List<states> s = {1,2,3,4}regID
                 {MA,NY,CT}state
                 {Suffolk,NY,Hampden}county

想要将县和州写入与列表中的 regID 匹配的 keyfrs 列表。到目前为止,我的程序所做的是解析两个文件并将每个文件写入它对应的不同类列表。如您所见,这两个类都有一个 regID 列。我需要做的是在 regID 上将两个列表匹配在一起,并将县和州写入 keyfrs 列表类,然后将该列表输出到一个新文件,其中包含这些添加的列。

  static void Main(string[] args)
    {

        PARSEkeYfrs();
        parsestateFile();
        matchValues();
          outputFile();

    }
    private static void outputFile()
    {
        string filename = @"c:\keyswCounty.csv";

        using(StreamWriter write = new StreamWriter(filename))
        {
            write.WriteLine("RegIF"+","+"ID"+","+"County"+","+"State");
            foreach(keyfrs k in keysandID)
            {
                write.WriteLine(k.regID +"," +k.ID+","+k.county+","+k.state);
            }

        }
    }

    private static void matchValues()
    {
       foreach(keyfrs k in keysandID)
       {


       }
    }

    private static void parsestateFile()
    {
        int a = 0;
       string filename = @"c:\ALLStates.txt";
        using (StreamReader read = new StreamReader(filename))
        {              
                read.ReadLine();
                while (!read.EndOfStream)
                {
                    a++;
                    try{
                    string line = read.ReadLine();
                    string[] splitline = line.Split(',');
                    if(splitline[1]!="")
                    {
                        states s = new states();
                        s.regID = Convert.ToInt64(splitline[0]);
                        s.county = Convert.ToString(splitline[1]);
                        s.state = Convert.ToString(splitline[2]);
                        stateFile.Add(s);
                    }
                    }
                    catch(Exception ex)
                    {
                       string.Format("error:{0}" + ex.Message.ToString());
                    }

                }

            }
    }

    private static void PARSEkeYfrs()
    { int a = 0;
        string filename = @"c:\key_frs.csv";
        using (StreamReader read = new StreamReader(filename))
        {              
                read.ReadLine();
                while (!read.EndOfStream)
                {
                    try{
                        a++;
                    string line = read.ReadLine();
                    string[] splitline = line.Split(',');
                    if(splitline[1]!="")
                    {
                        keyfrs k = new keyfrs();
                        k.regID = Convert.ToInt64(splitline[0]);
                        k.ID = Convert.ToInt64(splitline[1]);
                        k.county = "";
                        k.state = "";
                        keysandID.Add(k);
                    }
                    }
                    catch(Exception ex)
                    {
                        string.Format("error:{0}"+ex.Message.ToString());
                    }

                }

            }


        }
4

1 回答 1

0
switched the state list to a dictionary and matched the values by the key value pair by using the TryGetValue method.
于 2013-08-27T14:18:27.063 回答