gpt4 book ai didi

c# - Linq TotalCount 为 "Grandchildren",带有空检查和属性过滤器

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:41 25 4
gpt4 key购买 nike

给出以下类:

[DebuggerDisplay("CustomerKey = {CustomerKey}")]
public class Customer
{
public Customer()
{
this.Orders = new List<Order>();
}
public int CustomerKey { get; set; }
public ICollection<Order> Orders { get; set; }
}

[DebuggerDisplay("OrderKey = {OrderKey}, OrderDateOffset='{OrderDateOffset}'")]
public class Order
{
public Order()
{
this.OrderDetails = new List<OrderDetail>();
}
public int OrderKey { get; set; }
public Customer ParentCustomer { get; set; }
public DateTimeOffset OrderDateOffset { get; set; }
public ICollection<OrderDetail> OrderDetails { get; set; }
}

[DebuggerDisplay("OrderDetailKey='{OrderDetailKey}', ProductKey='{ProductKey}', Quantity='{Quantity}', UnitPrice='{UnitPrice}, BackOrdered='{BackOrdered}'")]
public class OrderDetail
{
public int OrderDetailKey { get; set; }
public Order ParentOrder { get; set; }
public int ProductKey { get; set; }
public int Quantity { get; set; }
public Decimal UnitPrice { get; set; }
public bool BackOrdered { get; set; }
}

考虑到以下水合作用:

            ICollection<Customer> customers = new List<Customer>();

Customer customerOne = new Customer() { CustomerKey = 11111 };
/**/
Order orderOne = null;
customerOne.Orders.Add(orderOne);/* note, the item added to the collection is null) */
/**/
Order orderTwo = new Order() { OrderKey = 22222, OrderDateOffset = DateTimeOffset.Now };
orderTwo.OrderDetails = null;/* Note, the child collection is null */
customerOne.Orders.Add(orderTwo);
/**/
Order orderThree = new Order() { OrderKey = 22223, OrderDateOffset = DateTimeOffset.Now };
orderThree.OrderDetails.Add(null); /* note, the item added to the collection is null) */
customerOne.Orders.Add(orderThree);
/**/
Order orderFour = new Order() { OrderKey = 22221, OrderDateOffset = DateTimeOffset.Now };
orderFour.OrderDetails.Add(new OrderDetail() { OrderDetailKey = 33333, ProductKey = 11, Quantity = 1, UnitPrice = 1M, BackOrdered = false });
orderFour.OrderDetails.Add(new OrderDetail() { OrderDetailKey = 33334, ProductKey = 12, Quantity = 2, UnitPrice = 2M, BackOrdered = false });
orderFour.OrderDetails.Add(new OrderDetail() { OrderDetailKey = 33335, ProductKey = 13, Quantity = 3, UnitPrice = 3M, BackOrdered = true });
customerOne.Orders.Add(orderFour);

customers.Add(customerOne);
customers.Add(null);/* note, the item added to the collection is null) */

我正在尝试获取所有延期交货的 OrderDetail 的总数。

我的 pre-linq 代码:

            int totalBackOrderedCount = 0;
if (null != customers)
{
foreach (Customer cust in customers)
{
if (null != cust)
{
if (null != cust.Orders)
{
foreach (Order ord in cust.Orders)
{
if (null != ord)
{
if (null != ord.OrderDetails)
{
foreach (OrderDetail ordDet in ord.OrderDetails)
{
if (null != ordDet)
{
if (ordDet.BackOrdered)
{
totalBackOrderedCount++;
}
}
else
{
Console.WriteLine("ordDet was null, good thing I coded for it");
}
}
}
else
{
Console.WriteLine("ord.OrderDetails was null, good thing I coded for it");
}
}
else
{
Console.WriteLine("ord was null, good thing I coded for it");
}
}
}
}
else
{
Console.WriteLine("cust was null, good thing I coded for it");
}
}
}

Console.WriteLine("totalBackOrderedCount={0}", totalBackOrderedCount);

我对 linq 替代方案的尝试:

            int linqyTotalBackOrderedCountOne =
(
from cust in customers
from ord in (cust.Orders ?? Enumerable.Empty<Order>()).DefaultIfEmpty()
from ordDet in (ord.OrderDetails.Where(od => od.BackOrdered == true) ?? Enumerable.Empty<OrderDetail>()).DefaultIfEmpty()
where cust != null && (null != ord) && (null != ordDet)
select ordDet
).Count();

int linqyTotalBackOrderedCountTwo =
(
from cust in customers
from ord in (cust.Orders ?? Enumerable.Empty<Order>()).DefaultIfEmpty()
from ordDet in (ord.OrderDetails.Where(od => od.BackOrdered == true) ?? Enumerable.Empty<OrderDetail>()).DefaultIfEmpty()
where cust != null && (null!=cust.Orders) && (null!=ord) && (null!=ord.OrderDetails) && (null!=ordDet)
select ordDet
).Count();

在 linq 中是否有一种“null friendly”的方式来获取 child 的 child ......?

最佳答案

你应该能够做这样的事情而不用担心空值。

var count = customers.Where (c => c.Orders != null)
.SelectMany (c => c.Orders.Where(o => o.OrderDetails != null)
.SelectMany (o => o.OrderDetails))
.Count (c => c.BackOrdered);

上面的很接近...这是最终答案:

            int itWorksCount = customers.Where(c => null != c && null != c.Orders)
.SelectMany(c => c.Orders.Where(o => null != o && null != o.OrderDetails)
.SelectMany(o => o.OrderDetails.Where(ordDet => null != ordDet)))
.Count(c => c.BackOrdered);

关于c# - Linq TotalCount 为 "Grandchildren",带有空检查和属性过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29929578/

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