Home > Net >  Linq LeftJoin Nullable object must have a value Error
Linq LeftJoin Nullable object must have a value Error

Time:07-08

When i use y in my select it has "Nullable object must have a value" error

 var products = productQuery.
        .GroupJoin(customerProductPrices,
        p => p.Id,
        pp => pp.ProductId,
                 (p, pp) => new { Product = p, CustomerProductPrice = pp })
                .SelectMany(
                   x => x.CustomerProductPrice.DefaultIfEmpty(),
                   (x, y) => new ProductFilterResultModel
                   {
                      Id = x.Product.Id,                           
                       Price = y != null  ? y.Price : 0
                   });

CodePudding user response:

I don't know what is your GroupJoin purpose? I don't have entities, from the example I can rewrite:

var products = customerProductPrices.Select(pp => new ProductFilterResultModel
{
    Id = pp.Product.Id,
    Price = pp != null ? pp.Price : 0
})

CodePudding user response:

I found the error, Thats because of my ProductFilterResultModel model, some properties in this model was not nullable but the datas which fetch from database and related to these properties was null. I change my nullable properties required type to the nullable type.

  • Related