Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#在一个列表中查找类类型,并将列表值写入另一个列表_C#_List_Class_Lookup_Exists - Fatal编程技术网

c#在一个列表中查找类类型,并将列表值写入另一个列表

c#在一个列表中查找类类型,并将列表值写入另一个列表,c#,list,class,lookup,exists,C#,List,Class,Lookup,Exists,我有两个列表保存到一个名为ListType的类中,该类显示在下面。我试图在匹配的类类型值上将两个列表相互匹配。这两个列表都包含county和act_loc类类型,所以我要做的是,文件列表中的county和act_loc等于普查文件中的county和act_loc,如果是这样,我想将所有类类型数据写入文件列表 ListType类: public class ListType { public ListType() { } public string ID { get

我有两个列表保存到一个名为ListType的类中,该类显示在下面。我试图在匹配的类类型值上将两个列表相互匹配。这两个列表都包含county和act_loc类类型,所以我要做的是,文件列表中的county和act_loc等于普查文件中的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);
    }
}

这是一个很长的问题,所以我不确定我是否回答了你的问题。但这里有一个尝试

在我看来,你很难找到正确的人口普查记录。如果是,这就是您想要的:

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”或类似的密钥。

尝试减少代码量。从这个角度看,似乎您添加了太多不相关的信息,这使得查看问题/代码的吸引力降低。也请阅读下面的内容。是的,这是可行的,但由于某些原因,写在人口普查中的记录不是他们应该做的。我正在逐步检查它将数据写入列表的位置及其正确性,但当它返回匹配的普查时,就像您在上面给我的一样,它并不是写入普查的内容。这很奇怪。
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;
    }
}
var cRecord = census.SingleOrDefault(p=>p.county == l.county && p.act_loc == l.act_loc);
if (cRecord != null) 
{
   l.black00 = cRecord.black00;
   // etc...
}