0

我有两个列表保存到一个名为 ListType 的类中,如下所示。我正在尝试在匹配类类型值时将两个列表相互匹配。两个列表都包含县和 act_loc 类类型,所以我要做的是 fileList 列表中的县和 act_loc 等于人口普查文件中的县和 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);
    }
}

Foreach ListType 县和 FileList 中的 act_loc 我想查看人口普查列表中是否存在同一个县和 act_loc(同一县和 act_loc 可能在 FileList 列表中存在多次,它只会在人口普查列表中出现一次),如果是的话我想将人口普查列表中的所有数据写入 FileList List。做这部分有困难。我想遍历 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;
    }
}
4

1 回答 1

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" 或类似的。

于 2013-09-15T23:21:23.273 回答