2013-08-18 73 views
-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()); 
        } 

       } 

      } 


     } 

回答

0
switched the state list to a dictionary and matched the values by the key value pair by using the TryGetValue method. 
相关问题