LINQ to SQL and Null Values

I have the following LINQ to SQL query:

var inTransitStocks = orderHistories.Where(oh => oh.Shipped_Qty > 0)
                                    .Select(oh => oh.Shipped_Qty); //.ToList();
var inTransitStock = (int)inTransitStocks.Sum();

Without a call, ToListI get an exception below in the line Sum():

A null value cannot be assigned to a member of type System.Double, which is a value type that is not null.

If I add .ToList()before the sum (as shown in the comment), I do not get an error.

Why am I getting the error in the first place? ( Shipped_Qtynon-null and no null data in db)

Why is a ToList()patch being added ?


The sql query is executed below (for a query larger than the above):

SELECT [t0].[Shipped Qty]
FROM [dbo].[Order History] AS [t0]
WHERE ([t0].[Shipped Qty] > @p0) AND ([t0].[CUST_ID] = @p1) AND ([t0].[SHIP_TO_ID] = @p2) AND ([t0].[Item] = @p3) AND (([t0].[DT_LST_SHP] >= @p4) OR (UNICODE([t0].[LN_STA]) = @p5))

Results are not returned.

+5
source share
3 answers

The reason is as follows:

ToList :

select SUM(Shipped_Qty) from orderHistories where Shipped_Qty > 0;

, , , 0, NULL.

ToList :

select Shipped_Qty from orderHistories where Shipped_Qty > 0;

( ) . . LINQ to Objects Sum. 0 NOT NULL.

, : .

+6

ToList() Linq-To-Objects Sum.

ToList(), Sum Linq-To-Sql.


, Linq-To-Sql, .

0

, : - var!

Without ToList() var <=> DbQuery
With ToList() var <=> List<Double>

Sum ...

0

All Articles