samreen khan
samreen khan

Reputation: 1

Left Outer Join with Nullable column

I want to make a query of LINQ with left outer join but with nullable column

YarnRequsitionDetailID is also null in other Yarn_PurchaseOrder_Details table and, I have to must apply to join on the base of it

  var yarnPOFilter_Grid = (from yrq in _context.Yarn_Requisition_Details
                                     join ypo in 
     _context.Yarn_PurchaseOrder_Details on yrq.YarnRequsitionDetailID equals 
      ypo.YarnRequsitionDetailID into t

     from rt in t.DefaultIfEmpty()  
                                     select new
                                     {
                                         YarnRequsitionDetailID = 
                                         rt.YarnRequsitionDetailID,
                                         yrq.YarnID,
                                         yrq.Yarn.YarnName,
                                         yrq.YarnFellowID,
                                         yrq.Yarn_FellowCodes.YarnFellowCode,
                                         yrq.QuantityRequired,
                                         rt.QuantityOrdered,
                                         QuantityBalance_Custom = 
      yrq.QuantityRequired - rt.QuantityOrdered
                                     }).ToList();
            return yarnPOFilter_Grid;

It give the following error

The cast to value type 'System.Double' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

Upvotes: 0

Views: 244

Answers (2)

Zev Spitz
Zev Spitz

Reputation: 15327

Any one of the fields in the joined entity which is of a non-nullable value type should be cast in the projection expression to the corresponding nullable type. For example, if QuantityRequired is a required field in the database:

var yarnPOFilter_Grid = (
        ...
        from rt in t.DefaultIfEmpty() select new {
            ...
            QuantityRequired = (double?)yrq.QuantityRequired,

Upvotes: 0

Gabriel Llorico
Gabriel Llorico

Reputation: 1803

Make your YarnRequsitionDetailID nullable if it is nullable so you can assign null to its property

because double cannot accept null but double? can accept null

Upvotes: 1

Related Questions