我有以下课程:
public class Order
{
public Order()
{
Items = new ObservableCollection<OrderItem>();
}
public string Code { get; set; }
public ICollection<OrderItem> Items { get; set; }
}
public class OrderItem
{
public OrderItem()
{
SubItems = new ObservableCollection<SubItem>();
}
public decimal Quantity { get; set; }
public decimal Price { get; set; }
public ICollection<SubItem> SubItems { get; set; }
}
public class SubItem
{
public DateTime Date { get; set; }
public decimal Quantity { get; set; }
public string UserName { get; set; }
}
现在我遇到的问题是我想在数据网格中显示这些类的所有数据,例如。使用代码:
Order order = new Order();
order.Code = "123";
order.Items.Add(new OrderItem()
{
Price = 30,
Quantity = 3,
SubItems = new Collection<SubItem>()
{
new SubItem() { Date = DateTime.Now, Quantity = 1, UserName = "User1" },
new SubItem() { Date = DateTime.Now, Quantity = 2, UserName = "User2" }
}
});
order.Items.Add(new OrderItem()
{
Price = 500,
Quantity = 50,
SubItems = new Collection<SubItem>()
{
new SubItem() { Date = DateTime.Now, Quantity = 20, UserName = "User1" },
new SubItem() { Date = DateTime.Now, Quantity = 20, UserName = "User2" },
new SubItem() { Date = DateTime.Now, Quantity = 10, UserName = "User3" }
}
});
我需要在 DataGrid 上显示如下内容:
| Order.Code | Item.Price | Item.Quantity | SubItem.Quantity | SubItem.UserName |
| 123 | 30 | 3 | 1 | User1 |
| 123 | 30 | 3 | 2 | User2 |
| 123 | 500 | 50 | 20 | User1 |
| 123 | 500 | 50 | 20 | User2 |
| 123 | 500 | 50 | 10 | User3 |
看起来很简单,但我就是做不到。我能做的最好的事情是引用OrderItem
onSubItem
以便我可以在列数据绑定上使用它,但这仅在我有 subItems 时才有效(如果没有SubItems
,我仍然需要显示OrderItem
数据)。所以基本上我需要显示如果我执行加入 3 个表的 SQL SELECT 所看到的确切内容。
任何 linq 魔法都可以做到这一点?