LINQ flattened parent affiliation graph

I am trying to get the number of parents without children and parents. As I write this, I understand that this is better explained by the code. So here it is:

In these examples:

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

And this data:

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

I am trying to return the score 3.

Thank you in advance for your help.

-Jessy Houle

ADD AFTER:

I was struck by all the big (and quick) answers. For others coming to this question looking for a few options, here is the Unit Test with a few working examples below.

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

Thank you again for your help!

+5
source share
6 answers

What about

int customerAndOrdersCount = customers.Sum(c => c.Orders==null ? 1 : Math.Max(1, c.Orders.Count()));
+5
source

If you initialize this Order property with an empty list instead of null, you can do the following:

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();
+1
customers
    .Select(customer => customer.Order)
    .Sum(orders => (orders != null) ? orders.Count() : 1)
+1

, " " 1 :

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

, .

+1

, , - :

 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
                    }
               );
0
source
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)  );

The result is the sum of these two values.

0
source

All Articles