一个实体包含多实体。
定义用户+订单。
1 public class Order { 2 public int RecordId { get; set; } 3 public string RecordName { get; set; } 4 public decimal Price { get; set; } 5 } 6 public class Person 7 { 8 public int RecordId { get; set; } 9 public string Name { get; set; } 10 11 //注意此 自动属性 的使用方法 12 public IList<Order> OrderList { get; set; } 13 }
调用:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //订单1 6 Order order1 = new Order(); 7 order1.RecordId = 1; 8 order1.RecordName = "语文"; 9 order1.Price = 45.32M; 10 11 //订单2 12 Order order2 = new Order(); 13 order2.RecordId = 2; 14 order2.RecordName = "数学"; 15 order2.Price = 55.67M; 16 17 //订单集合 18 List<Order> orderList = new List<Order>(); 19 orderList.Add(order1); 20 orderList.Add(order2); 21 22 //购买订单者:张三 23 Person person = new Person(); 24 person.Name = "张三"; 25 person.RecordId = 1; 26 person.OrderList = orderList; 27 28 //打印张三订单的信息 29 Console.WriteLine("购买者编号:" + person.RecordId + ";购买者:" + person.Name); 30 Console.WriteLine("购买的教程:" + person.OrderList[0].RecordName + ";" + person.OrderList[1].RecordName); 31 32 Console.ReadKey(); 33 } 34 }
打印: