从多个数据集中填充 class 个对象
Filling class object from multiple dataset
我想用来自 OrganismDT 和 AntibioticDT[= 的数据填充 Organism class 对象28=]。下面是数据table结构,Class结构和我正在尝试的填充技术。基于 OrganismAliasId 我必须填写 AntibioticsDT[Antibiotic objects list] 行并分配给相应的 Organisms 目的。任何人都可以帮助我实现这一点..
数据表
DataTable OrganismDT = new DataTable("Organism");
OrganismDT.Columns.Add("OrganismAliasId");
OrganismDT.Columns.Add("Name");
DataTable AntibioticsDT = new DataTable("Antibiotics");
AntibioticsDT.Columns.Add("OrganismAliasId");
AntibioticsDT.Columns.Add("Name");
AntibioticsDT.Columns.Add("Susceptibility");
Class结构:
public class Organism
{
public int AliasId { get; set; }
public string Name { get; set; }
public List<Antibiotic> Antibiotics;
}
public class Antibiotic
{
public string Name { get; set; }
public string Susceptibility { get; set; }
}
填充数据table table 到 Class 对象
List<Organism> Organisms = Organism.AsEnumerable().Select(row =>
new Organism
{
AliasId = row.Field<int>("OrganismAliasId"),
Name = row.Field<string>("Name"),
Antibiotics = new List<Antibiotic>()
}).ToList();
这应该有效:-
List<Organism> Organisms = OrganismDT.AsEnumerable().Select(row =>
new Organism
{
AliasId = row.Field<int>("OrganismAliasId"),
Name = row.Field<string>("Name"),
Antibiotics = AntibioticsDT.AsEnumerable()
.Where(x => x.Field<int>("OrganismAliasId")
== row.Field<int>("OrganismAliasId"))
.Select(x => new Antibiotic
{
Name = x.Field<string>("Name"),
Susceptibility = x.Field<string>("Susceptibility")
}).ToList()
}).ToList();
此外,请注意您应该通过指定数据类型来创建列,否则它可能会抛出 运行-time 异常:-
AntibioticsDT.Columns.Add("OrganismAliasId",typeof(int));
我想用来自 OrganismDT 和 AntibioticDT[= 的数据填充 Organism class 对象28=]。下面是数据table结构,Class结构和我正在尝试的填充技术。基于 OrganismAliasId 我必须填写 AntibioticsDT[Antibiotic objects list] 行并分配给相应的 Organisms 目的。任何人都可以帮助我实现这一点..
数据表
DataTable OrganismDT = new DataTable("Organism");
OrganismDT.Columns.Add("OrganismAliasId");
OrganismDT.Columns.Add("Name");
DataTable AntibioticsDT = new DataTable("Antibiotics");
AntibioticsDT.Columns.Add("OrganismAliasId");
AntibioticsDT.Columns.Add("Name");
AntibioticsDT.Columns.Add("Susceptibility");
Class结构:
public class Organism
{
public int AliasId { get; set; }
public string Name { get; set; }
public List<Antibiotic> Antibiotics;
}
public class Antibiotic
{
public string Name { get; set; }
public string Susceptibility { get; set; }
}
填充数据table table 到 Class 对象
List<Organism> Organisms = Organism.AsEnumerable().Select(row =>
new Organism
{
AliasId = row.Field<int>("OrganismAliasId"),
Name = row.Field<string>("Name"),
Antibiotics = new List<Antibiotic>()
}).ToList();
这应该有效:-
List<Organism> Organisms = OrganismDT.AsEnumerable().Select(row =>
new Organism
{
AliasId = row.Field<int>("OrganismAliasId"),
Name = row.Field<string>("Name"),
Antibiotics = AntibioticsDT.AsEnumerable()
.Where(x => x.Field<int>("OrganismAliasId")
== row.Field<int>("OrganismAliasId"))
.Select(x => new Antibiotic
{
Name = x.Field<string>("Name"),
Susceptibility = x.Field<string>("Susceptibility")
}).ToList()
}).ToList();
此外,请注意您应该通过指定数据类型来创建列,否则它可能会抛出 运行-time 异常:-
AntibioticsDT.Columns.Add("OrganismAliasId",typeof(int));