gpt4 book ai didi

c# - LINQ 查询以使用子详细信息展平主/详细信息

转载 作者:行者123 更新时间:2023-11-30 13:32:17 24 4
gpt4 key购买 nike

我有以下类(class):

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 的引用,这样我就可以在列数据绑定(bind)上使用它,但这只有在我有子项目时才有效(如果有不是 SubItems,我仍然需要显示 OrderItem 数据)。所以基本上我需要显示我在执行连接 3 个表的 SQL SELECT 时看到的确切内容。

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

最佳答案

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

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

关于c# - LINQ 查询以使用子详细信息展平主/详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com