4

我有以下课程:

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           |

看起来很简单,但我就是做不到。我能做的最好的事情是引用OrderItemonSubItem以便我可以在列数据绑定上使用它,但这仅在我有 subItems 时才有效(如果没有SubItems,我仍然需要显示OrderItem数据)。所以基本上我需要显示如果我执行加入 3 个表的 SQL SELECT 所看到的确切内容。

任何 linq 魔法都可以做到这一点?

4

2 回答 2

7

使用SelectMany方法进行展平:

var res = order.Items
               .SelectMany(i => i.SubItems, (Item, Sub) => new { Item, Sub })
               .Select(r => new { order.Code, 
                                  r.Item.Price, 
                                  ItemQuantity = r.Item.Quantity, 
                                  SubItemQuantity = r.Sub.Quantity, 
                                  r.Sub.UserName });

如果SubItems集合可能为空,您可以使用DefaultIfEmpty方法:

var res = order.Items
               .SelectMany(i => i.SubItems.DefaultIfEmpty(), (Item, Sub) => new { Item, Sub })
               .Select(r => new { order.Code, 
                                  r.Item.Price, 
                                  ItemQuantity = r.Item.Quantity, 
                                  SubItemQuantity = r.Sub == null ? null : (Decimal?) r.Sub.Quantity ,
                                  UserName = r.Sub == null ? (string)null : r.Sub.UserName });

试试Ideone

于 2013-02-12T11:33:02.570 回答
6

试试这个,它就像一个连接:

var table = from item in order.Items
            from subItem in item.SubItems
            select new
                {
                    OrderCode = order.Code,
                    ItemPrice = item.Price,
                    ItemQuantity = item.Quantity,
                    SubItemQuantity = subItem.Quantity,
                    SubItemUserName = subItem.UserName
                };

如果您愿意,可以将其扩展到多个订单:

var table = from order in orders
            from item in order.Items
            from subItem in item.SubItems
            select new
                {
                    OrderCode = order.Code,
                    ItemPrice = item.Price,
                    ItemQuantity = item.Quantity,
                    SubItemQuantity = subItem.Quantity,
                    SubItemUserName = subItem.UserName
                };

这是一个链接: http: //msdn.microsoft.com/en-us/library/bb383978.aspx (请参阅条款中的复合

如果集合可以为空,请使用 DefaultIfEmpty(),例如:

var table = from order in orders
            from item in order.Items.DefaultIfEmpty(new OrderItem())
            from subItem in item.SubItems.DefaultIfEmpty(new SubItem())
            select new
                {
                    OrderCode = order.Code,
                    ItemPrice = item.Price,
                    ItemQuantity = item.Quantity,
                    SubItemQuantity = subItem.Quantity,
                    SubItemUserName = subItem.UserName
                };

如果您不向 DefaultIfEmpty() 传递参数,则“默认”项将为空,因此您必须处理它,例如:

var table = from order in orders
            from item in order.Items.DefaultIfEmpty()
            from subItem in (item != null ? item.SubItems : Enumerable.Empty<SubItem>()).DefaultIfEmpty()
            select new
                {
                    OrderCode = order.Code,
                    ItemPrice = item != null ? item.Price.ToString() : "n/a",
                    ItemQuantity = item != null ? item.Quantity.ToString() : "n/a",
                    SubItemQuantity = subItem != null ? subItem.Quantity.ToString() : "n/a",
                    SubItemUserName = subItem != null ? subItem.UserName : "n/a"
                };

另一种选择:

var table = from order in orders
            from item in order.Items.DefaultIfEmpty()
            from subItem in (item != null ? item.SubItems : Enumerable.Empty<SubItem>()).DefaultIfEmpty()
            select new
                {
                    OrderCode = order.Code,
                    ItemPrice = item != null ? item.Price : default(decimal?),
                    ItemQuantity = item != null ? item.Quantity : default(decimal?),
                    SubItemQuantity = subItem != null ? subItem.Quantity : default(decimal?),
                    SubItemUserName = subItem != null ? subItem.UserName : null
                };
于 2013-02-12T11:26:47.533 回答