我正在从DB中检索关系数据集(2个表)。
我有一个如下定义的DataContract:
public class DetailData
{
[DataMember]
public Customer_Detail Detail;
[DataMember]
public Relationship_Detail Relationship_Detail;
}
[DataContract(Name = "Detail")]
public class Customer_Detail
{
[DataMember]
public string ID { get; set; }
[DataMember]
public string NAME { get; set; }
}
[DataContract(Name = "Relationship Detail")]
public class Relationship_Detail
{
[DataMember]
public string ID { get; set; }
[DataMember]
public string STATUS { get; set; }
}
现在在我的数据访问层中,我需要将数据集table1值分配给“Customer_Detail”,将第二个表值分配给“Relationship_Detail”。
我怎样才能做到这一点?
提前感谢您的帮助。
答案 0 :(得分:0)
像这样 -
obj.Prop1 = ds.Tables[0].Rows.Cast<DataRow>().Select(a => new Test{ Name = a.Field<int>("Name"), Age = a.Field<string>("Age") }).ToList();
obj.Prop2 = ds.Tables[1].Rows.Cast<DataRow>().Select(a => new Test2{ Name2 = a.Field<int>("Name"), Age2 = a.Field<string>("Age") }).ToList();