将一个列表中的值与另一个列表匹配,并将值写入其他列表

时间:2013-08-18 23:21:13

标签: c# linq list lambda

我有一个写入此类的列表:

  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());
                    }

                }

            }


        }

1 个答案:

答案 0 :(得分:0)

switched the state list to a dictionary and matched the values by the key value pair by using the TryGetValue method.