提供以下课程:
[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的总数。
我的前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();
是否有&#34; null友好&#34;在linq的方式让孩子的孩子......?
答案 0 :(得分:1)
你应该可以做这样的事情,而不必担心空值。
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);
答案 1 :(得分:1)
ICollection<T>
属性不是null
。当你也可以拥有一个空集合(你甚至在构造函数中实例化,所以你不保存内存)时,绝对没有理由在那里有null
值。
重新设计您的Customer
和Order
类,以便Orders
和OrderDetails
属性的setter是私有的。您的序列化应该能够处理这个(实体框架/ NHibernate / Data Contracts至少这样做)。这可以防止其他代码将集合设置为null
。
另外,为什么允许在集合中插入null
值? null
值代表什么。是否有未填写的订单,占位符?如果是这种情况,请添加一些代表缺少订单/订单明细的值。
修复设计后,请使用Gary。 S&s的原始答案:
int count = customers.SelectMany(c => c.Orders)
.SelectMany(o => o.OrderDetails)
.Count(od => od.BackOrdered);
请注意,总是有一种无效的友好方式来处理所有事情,但它会使代码过于复杂且难以维护。