C# 将一个列表中的值与另一个列表中的值匹配,并将值写入另一个列表
我有一个清单是写给这个班的:C# 将一个列表中的值与另一个列表中的值匹配,并将值写入另一个列表,c#,linq,list,lambda,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}]
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
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());
}
}
}
}
switched the state list to a dictionary and matched the values by the key value pair by using the TryGetValue method.