LINQ, Left Join, throwing exception ... failed because the materialized value is null

I'm at a standstill, how can I fix this? In the ProductAvailability table in my query there is no record for each product found, and every time I start it, the following error occurs.

An argument of type "DateTime" failed because the materialized value is null. Either the general parameter of the result type, or the query should use a type with a null value.

How to fix this error? I tried casting paj.DateAvailable (DateTime?), And also checked null, but this does not seem to fix the problem. Hmm?

Here is my request. Any ideas?

var query = (from p in entities.Products
             join pa in entities.ProductAvailabilities on p.ProductId equals pa.ProductId into joinProductAvailabilities
             from paj in joinProductAvailabilities.DefaultIfEmpty()
             where  ps.IsActive
             select new { ProductId = p.ProductId, DateAvailable = paj.DateAvailable }).Distinct();
+5
source share
1 answer

.

public class MyProductQueryResult
{
  public int ProductId {get;set;}
  public DateTime? DateAvailable {get;set;}
}


from p in entities.Products 
from paj in p.ProductAvailabilities.DefaultIfEmpty()
select new MyProductQueryResult()
  {ProductId = p.ProductId, DateAvailable = paj.DateAvailable}

, . , , paj.DateAvailable , - .

+1

All Articles