5

我正在尝试计算没有孩子的父母加上父母的孩子。当我写这篇文章时,我意识到用代码更好地解释它。所以,这里是:

使用这些示例类型:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public string Description { get; set; }
}

而这个数据:

var customers = new List<Customer>
{
    new Customer
    {
        Id = 2,
        Name = "Jane Doe"
    },
    new Customer
    {
        Id = 1,
        Name = "John Doe",
        Orders = new List<Order>
        {
            new Order { Id = 342, Description = "Ordered a ball" },
            new Order { Id = 345, Description = "Ordered a bat" }
        }
    }
};

// I'm trying to get a count of customer orders added with customers with no orders
// In the above data, I would expect a count of 3 as detailed below
//
// CId      Name        OId
// ----     --------    ----
//  2       Jane Doe
//  1       John Doe    342
//  1       John Doe    345

int customerAndOrdersCount = {linq call here}; // equals 3

我正在尝试数 3 回来。

预先感谢您的帮助。

-杰西·豪尔

之后添加:

我对所有伟大(和快速)的答案印象深刻。对于其他人来这个问题,寻找一些选项,这里是一个单元测试,下面有一些工作示例。

[TestMethod]
public void TestSolutions()
{
    var customers = GetCustomers(); // data from above

    var count1 = customers.Select(customer => customer.Orders).Sum(orders => (orders != null) ? orders.Count() : 1);
    var count2 = (from c in customers from o in (c.Orders ?? Enumerable.Empty<Order>() ).DefaultIfEmpty() select c).Count();
    var count3 = customers.Sum(c => c.Orders == null ? 1 : c.Orders.Count());
    var count4 = customers.Sum(c => c.Orders==null ? 1 : Math.Max(1, c.Orders.Count()));


    Assert.AreEqual(3, count1);
    Assert.AreEqual(3, count2);
    Assert.AreEqual(3, count3);
    Assert.AreEqual(3, count4);
}

再次感谢大家的帮助!

4

6 回答 6

5

怎么样

int customerAndOrdersCount = customers.Sum(c => c.Orders==null ? 1 : Math.Max(1, c.Orders.Count()));
于 2012-09-06T14:51:49.667 回答
1

如果您想将“无订单”计为 1 并计算订单,则此方法有效:

int customerOrders = customers.Sum(c => c.Orders == null ? 1 : c.Orders.Count());

顺便说一句,这个问题非常典型。

于 2012-09-06T15:11:08.020 回答
1

如果您将使用空列表而不是 null 来初始化 Order 属性,则可以执行以下操作:

int count =
  (
    from c in customers
    from o in c.Orders.DefaultIfEmpty()
    select c
  ).Count();

如果您决定保留未初始化的属性,则改为:

int count =
  (
    from c in customers
    from o in (c.Orders ?? Enumerable.Empty<Order>() ).DefaultIfEmpty()
    select c
  ).Count();
于 2012-09-06T15:02:24.143 回答
1
customers
    .Select(customer => customer.Order)
    .Sum(orders => (orders != null) ? orders.Count() : 1)
于 2012-09-06T15:04:50.210 回答
0

您可能正在搜索这样的内容:

 customers.GroupBy(customer=>customer).  //group by object iyself
        Select(c=>                       //select
                    new  
                    {
                      ID = c.Key.Id,                             
                      Name = c.Key.Name, 
                      Count = (c.Key.Orders!=null)? c.Key.Orders.Count():0
                    }
               );
于 2012-09-06T14:58:06.610 回答
0
var orderFreeCustomers = customers.Where(c=>c.Orders== null || c.Orders.Any()==false);

var totalOrders = customers.Where (c => c.Orders !=null).
Aggregate (0,(v,e)=>(v+e.Orders.Count)  );

结果是这两个值的总和

于 2012-09-06T15:09:38.060 回答