我有两个列表保存到名为ListType的类中,如下所示。我正在尝试在匹配的类类型值上将两个列表相互匹配。两个列表都包含county和act_loc类类型,因此我要做的是来自fileList列表的county和act_loc等于来自census文件的county和act_loc,如果是,我想将所有类类型数据写入文件列表。
ListType类:
public class ListType
{
public ListType()
{
}
public string ID { get; set; }
public string REGSID { get; set; }
public string county { get; set; }
public string act_loc { get; set; }
public string hmonth { get; set; }
public string hisnc { get; set; }
public string hinc { get; set; }
public string black00 {get; set;}
public string asian00 { get; set; }
public string hispanic00 { get; set; }
public string pop2000 { get; set; }
public string popden2000 { get; set; }
public string totalunder18_00 { get; set; }
public string sixtyfiveplus_00 { get; set; }
public string percentforeign_00 { get; set; }
public string percentsixteenplusinmanu_00 { get; set; }
public string medhouseholdinc_00 { get; set; }
public string totalbelowPOV_00 { get; set; }
public string englishverywell_00 { get; set; }
public string black10 { get; set; }
public string asian10 { get; set; }
public string hispanic10 { get; set; }
public string pop2010 { get; set; }
public string popden2010 { get; set; }
public string totalunder18_10 { get; set; }
public string sixtyfiveplus_10 { get; set; }
public string percentforeign_10 { get; set; }
public string percentsixteenplus_10 { get; set; }
public string medhouseholdinc_10 { get; set; }
public string totalbelowPOV_10 { get; set; }
public string englishverywell_10 { get; set; }
public string etype { get; set; }
public string etypeText { get; set; }
public string edate { get; set; }
public string fmp_amt { get; set; }
public string newenftype_text { get; set; }
public string finalenftype_text { get; set; }
}
}
解析为列表的一个文件:
public static List<ListType> census = new List<Listtype>();
using (StreamReader read = new StreamReader(Filename))
{
read.ReadLine();
while (!read.EndOfStream)
{
string line = read.ReadLine();
string[] splitline = line.Split(',');
ListType l = new ListType();
l.county = splitline[0].ToString();
l.act_loc = splitline[1].ToString();
l.black00 = splitline[2].ToString();
l.asian00 = splitline[3].ToString();
l.hispanic00 = splitline[4].ToString();
l.pop2000 = splitline[5].ToString();
l.popden2000 = splitline[6].ToString();
l.totalunder18_00 = splitline[7].ToString();
l.sixtyfiveplus_00 = splitline[8].ToString();
l.percentforeign_00 = splitline[9].ToString();
l.percentsixteenplusinmanu_00 = splitline[10].ToString();
l.medhouseholdinc_00 = splitline[11].ToString();
l.totalbelowPOV_00 = splitline[12].ToString();
l.englishverywell_00 = splitline[13].ToString();
l.hispanic10 = splitline[14].ToString();
l.black00 = splitline[15].ToString();
l.asian00 = splitline[16].ToString();
l.sixtyfiveplus_10 = splitline[17].ToString();
l.totalunder18_10 = splitline[18].ToString();
l.englishverywell_10 = splitline[19].ToString();
l.percentforeign_10 = splitline[20].ToString();
l.percentsixteenplus_10 = splitline[21].ToString();
l.medhouseholdinc_10 = splitline[22].ToString();
l.totalbelowPOV_10 = splitline[23].ToString();
l.pop2010 = splitline[24].ToString();
l.popden2010 = splitline[25].ToString();
census.Add(l);
}
}
解析为FileList列表的其他文件:
public static List,ListType> FileList = new List<ListType>();
using (StreamReader read = new StreamReader(Filename))
{
read.ReadLine();
while (!read.EndOfStream)
{
a++;
string line = read.ReadLine();
string[] splitline = line.Split(',');
ListType l = new ListType();
l.ID = splitline[0].ToString();
l.REGSID = splitline[1].ToString();
l.act_loc = l.ID.Substring(0, 2);
l.county = splitline[2].ToString();
l.hmonth = splitline[4].ToString();
l.hisnc = splitline[5].ToString();
l.hinc = splitline[6].ToString();
l.etype = splitline[7].ToString();
l.etypeText = splitline[8].ToString();
l.edate = splitline[9].ToString();
l.fmp_amt = splitline[10].ToString();
l.finalenftype_text = splitline[11].ToString();
fileList.Add(l);
}
}
FileList中的Foreach ListType县和act_loc我想查看人口普查列表中是否存在同一个县和act_loc(同一个县和act_loc可以在FileList列表中多次出现,它只会在人口普查列表中出现一次)如果是这样的话我想将人口普查列表中的所有数据写入文件列表列表。做这部分有困难。我想循环遍历FileList中的每条记录,如果它存在于人口普查列表中,我想将人口普查数据写入FileList记录,但是当我写入数据时,下面的人口普查不存在如何访问人口普查行,以便我可以将它写入fileList列表。
foreach (ListType l in fileList)
{
bool exists = census.Exists(p => p.county == l.county && p.act_loc == l.act_loc);
if (exists)
{
//census data record to matching FileList record
l.black00 = census.black00;
l.asian00 = census.asian00;
l.hispanic00 = census.hispanic00;
l.pop2000 = census.pop2000;
l.popden2000 = census.popden2000;
l.totalunder18_00 = census.totalunder18_00;
l.sixtyfiveplus_00 = census.sixtyfiveplus_00;
l.percentforeign_00 = census.percentforeign_00;
l.percentsixteenplusinmanu_00 = census.percentsixteenplusinmanu_00;
l.medhouseholdinc_00 = census.medhouseholdinc_00;
l.totalbelowPOV_00 = census.totalbelowPOV_00;
l.englishverywell_00 = census.englishverywell;
l.hispanic10 = census.hispanic10;
l.black00 = census.black10;
l.asian00 = census.asian10;
l.sixtyfiveplus_10 = census.sixtyfiveplus_10;
l.totalunder18_10 = census.totalunder18_10;
l.englishverywell_10 = census.englishverywell_10;
l.percentforeign_10 = census.percentforeign_10;
l.percentsixteenplus_10 = census.percentsixteenplus_10;
l.medhouseholdinc_10 = census.medhouseholdinc_10;
l.totalbelowPOV_10 = census.totalbelowPOV_10;
l.pop2010 = census.pop2010;
l.popden2010 = census.popden2010;
}
}
答案 0 :(得分:0)
这是一个冗长的问题,所以我不确定我是在回答你的问题。但这是一次尝试。
在我看来,你在检索正确的人口普查记录方面遇到了麻烦。如果是这样,这就是你想要的:
var cRecord = census.SingleOrDefault(p=>p.county == l.county && p.act_loc == l.act_loc);
if (cRecord != null)
{
l.black00 = cRecord.black00;
// etc...
}
此外,我可以建议您使用字典作为数据,以便查找比使用linq查询更容易吗?唯一的挑战是你有一个由两个字段组成的密钥。然后,您可以为两个值的键创建一个结构,或者只使用带有像管道这样的分隔符的字符串键。因此,如果你的密钥是county =“mycounty”和act_loc =“myactloc”,你可以创建一个密钥“mycounty | myactloc”或者其他一些密钥。