LINQ查询,用子详细信息展开主/详细信息

  • 本文关键字:详细信息 查询 LINQ c# linq
  • 更新时间 :
  • 英文 :


我有以下类:

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           |

看起来很简单,但我就是做不到。我能做的最好的事情是在SubItem上放置对OrderItem的引用,这样我就可以在列数据绑定中使用它,但只有当我有子项时才有效(如果没有SubItems,我仍然需要显示OrderItem数据)。因此,基本上,如果我执行一个连接3个表的SQL SELECT,我需要显示我看到的确切内容。

任何林克魔法都能做到这一点?

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

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(),那么"默认"项将为null,所以你必须处理它,例如:

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
                };

使用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。

相关内容

  • 没有找到相关文章

最新更新