Linq Select New List Property Null Check

c#, linq, null

Solution

you can check for null using ternary operator like this:

var productTypes = from ProductDto e in Product
                            select new 
                            {
                                Id = e.Product != null ? e.Product.ID : 0, 
                                Name = "xyz"
                            };

Problem

I have a below LINQ query : ``` var productTypes = from ProductDto e in Product select new { Id = e.Product.ID, Name = e.Product.name }; ``` In the above LINQ query the e.Product may be `null`.But I am not getting the way to find out. Can anyone help me out ? I want to assign `null` in productTypes variable if e.Product is `null`.

Original source