Exception handling in Linq to SQL for customers without orders

Posted by stackoverflowuser on Stack Overflow See other posts from Stack Overflow or by stackoverflowuser
Published on 2010-05-22T00:50:39Z Indexed on 2010/05/22 1:50 UTC
Read the original article Hit count: 263

Filed under:
|
|

I have the following code to retrieve customer name, total (orders ), sum (order details) for reach customer in Northwind database. The problem with below code is that it raises an exception since a few customers dont have any entry in orders table.

I know using the query syntax (join) the exception can be avoided. I want to know if the same can be handled with the extension method syntax.

 var customerOrders = db.Customers
    .Select(c => new 
    { 
        CompanyName = c.CompanyName, 
        TotalOrders = c.Orders.Count(), 
        TotalQuantity = c.Orders
            .SelectMany(o => o.Order_Details).Sum(o=>o.Quantity) 
    });

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ