Re: Linq Sum() Question



<Olivier.Paesbrugghe@xxxxxxxxx> wrote in message news:1189683757.167406.72170@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm having a problem with a Linq statement.
I made a console application that makes use of Northwind database.
Made a dbml file of the database whith the designer and now I want to
see the total of all orders of the customers.
I used this statement:

var custTotalOrders = db.Customers
.Select( p =>
new { c.ContactName,
TotalOrders = c.Orders.Sum( o => o.Order_Details.Sum( od =>
od.Quantity * od.UnitPrice))});

(Un)fortunately there is 1 client who hasn't made any orders yet and
so I get an error:

Unhandled Exception: System.InvalidOperationException: The null value
cannot be
assigned to a member with type System.Decimal which is a non-nullable
value type.

Is there something wrong with my expression or is this an error due to
the fact LINQ is still in beta?

kind regards,

Olivier


Supposing UnitPrice is a 'decimal' type...
TotalOrders = c.Orders.Sum( o => (decimal?) o.Order_Details.Sum( od =>...

or only select the customer who have orders:

db.Customers
.Where(q => q.Orders.Count > 0)
.Select(...


Willy.

.



Relevant Pages

  • Re: SQL 2005 - audit SELECT statements?
    ... name in the Northwind database. ... Start the trace, and create the following trigger using Query Analyzer: ... FROM Customers ...
    (microsoft.public.sqlserver.security)
  • Re: Linq Sum() Question
    ... I made a console application that makes use of Northwind database. ... Made a dbml file of the database whith the designer and now I want to ... the fact LINQ is still in beta? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: i would like to see who executed a select statement on a specific table
    ... name in the Northwind database. ... and create the following trigger using Query Analyzer: ... FROM Customers ... FROM Customers c INNER JOIN Orders o ...
    (microsoft.public.sqlserver.security)
  • Re: Why cant i find a row in a dataset?
    ... > Currently had a Dataset that has information from the Northwind database, ... > Customers table in it. ... why when I enter ALFKI in txtCustomerID.Text is it not ... > Paul Custance ...
    (microsoft.public.dotnet.framework)
  • Re: Coding file into 4ths
    ... Customers in the Northwind database based on their customer id: ... MS Access MVP ... >> HTH, ...
    (microsoft.public.access.queries)