将列表中的名称与 class 中的元素匹配
Match names in list with elements in class
我想知道是否有任何方法可以将列表中的名称与 class:
中的元素相匹配
我有一个 class:
public class exampleClass
{
public string name { get; set; }
public string value { get; set; }
}
和一个列表:List<exampleClass> EnfSist
这就是制作清单的方式。现在我想知道如何从我的列表中匹配或识别 "name" 内的字符串。要匹配这个 class:
tbl_sistematicas b = new tbl_sistematicas
{
ap_enf_id_enfermedad = Convert.ToInt32(EnfSist[0].value),
ap_pac_inicio = Convert.ToInt32(EnfSist[1].value),
ap_pac_inicio_periodo = Convert.ToInt32(2].value),
ap_pac_duracion = Convert.ToInt32(EnfSist[3].value),
ap_pac_duracion_periodo = Convert.ToInt32(EnfSist[4].value),
ap_pac_tratamiento = EnfSist[5].value
};
一旦能够匹配相同的名称,我就不必指定列表中每个元素的每个索引。列表中的元素与 table 中的元素同名。并非 class 的所有元素都被使用。
我有这样的东西:tbl_sistematicas bh = EnfSist.FindAll(x => x.name == bh.?????? );
如果我理解这个问题,你可以使用像 automapper 或 ValueInjector
这样的东西来做到这一点
一个使用 ValueInjector 的例子
void Main()
{
List<exampleClass> EnfSist = new List<exampleClass>();
EnfSist.Add(new exampleClass { name = "ap_enf_id_enfermedad", value = "12" });
EnfSist.Add(new exampleClass { name = "apap_pac_inicio" , value = "34" });
// etc
tbl_sistematicas b = new tbl_sistematicas();
b.InjectFrom<MyInjection>(EnfSist);
}
public class MyInjection : KnownSourceValueInjection<List<exampleClass>>
{
protected override void Inject(List<exampleClass> source, object target)
{
foreach(var entry in source)
{
var property = target.GetProps().GetByName(entry.name, true);
if (property != null)
property.SetValue(target, Convert.ChangeType(entry.value, property.PropertyType));
}
}
}
public class exampleClass
{
public string name { get; set; }
public string value { get; set; }
}
public class tbl_sistematicas
{
public int ap_enf_id_enfermedad { get; set; }
public int apap_pac_inicio { get; set; }
public int ap_pac_inicio_periodo { get; set; }
public int ap_pac_duracion { get; set; }
public int ap_pac_duracion_periodo { get; set; }
public string ap_pac_tratamiento { get; set; }
}
注意,如果值不能转换为 int,这将抛出异常
我想知道是否有任何方法可以将列表中的名称与 class:
中的元素相匹配我有一个 class:
public class exampleClass
{
public string name { get; set; }
public string value { get; set; }
}
和一个列表:List<exampleClass> EnfSist
这就是制作清单的方式。现在我想知道如何从我的列表中匹配或识别 "name" 内的字符串。要匹配这个 class:
tbl_sistematicas b = new tbl_sistematicas
{
ap_enf_id_enfermedad = Convert.ToInt32(EnfSist[0].value),
ap_pac_inicio = Convert.ToInt32(EnfSist[1].value),
ap_pac_inicio_periodo = Convert.ToInt32(2].value),
ap_pac_duracion = Convert.ToInt32(EnfSist[3].value),
ap_pac_duracion_periodo = Convert.ToInt32(EnfSist[4].value),
ap_pac_tratamiento = EnfSist[5].value
};
一旦能够匹配相同的名称,我就不必指定列表中每个元素的每个索引。列表中的元素与 table 中的元素同名。并非 class 的所有元素都被使用。
我有这样的东西:tbl_sistematicas bh = EnfSist.FindAll(x => x.name == bh.?????? );
如果我理解这个问题,你可以使用像 automapper 或 ValueInjector
这样的东西来做到这一点一个使用 ValueInjector 的例子
void Main()
{
List<exampleClass> EnfSist = new List<exampleClass>();
EnfSist.Add(new exampleClass { name = "ap_enf_id_enfermedad", value = "12" });
EnfSist.Add(new exampleClass { name = "apap_pac_inicio" , value = "34" });
// etc
tbl_sistematicas b = new tbl_sistematicas();
b.InjectFrom<MyInjection>(EnfSist);
}
public class MyInjection : KnownSourceValueInjection<List<exampleClass>>
{
protected override void Inject(List<exampleClass> source, object target)
{
foreach(var entry in source)
{
var property = target.GetProps().GetByName(entry.name, true);
if (property != null)
property.SetValue(target, Convert.ChangeType(entry.value, property.PropertyType));
}
}
}
public class exampleClass
{
public string name { get; set; }
public string value { get; set; }
}
public class tbl_sistematicas
{
public int ap_enf_id_enfermedad { get; set; }
public int apap_pac_inicio { get; set; }
public int ap_pac_inicio_periodo { get; set; }
public int ap_pac_duracion { get; set; }
public int ap_pac_duracion_periodo { get; set; }
public string ap_pac_tratamiento { get; set; }
}
注意,如果值不能转换为 int,这将抛出异常