title: Can I project an optional reference of an entity into an optional reference of the projection's result type?
body: <p>Say, I have two entities:</p>
  
  <pre><code>public class Customer
  {
      public int Id { get; set; }
      public int SalesLevel { get; set; }
      public string Name { get; set; }
      public string City { get; set; }
  }
  
  public class Order
  {
      public int Id { get; set; }
      public DateTime DueDate { get; set; }
      public string ShippingRemark { get; set; }
  
      public int? CustomerId { get; set; }
      public Customer Customer { get; set; }
  }
  </code></pre>
  
  <p><code>Customer</code> is an <em>optional</em> (nullable) reference in <code>Order</code> (maybe the system supports "anonymous" orders).</p>
  
  <p>Now, I want to project some properties of an order into a view model including some properties of the customer <strong>if</strong> the order has a customer. I have two view model classes then:</p>
  
  <pre><code>public class CustomerViewModel
  {
      public int SalesLevel { get; set; }
      public string Name { get; set; }
  }
  
  public class OrderViewModel
  {
      public string ShippingRemark { get; set; }
      public CustomerViewModel CustomerViewModel { get; set; }
  }
  </code></pre>
  
  <p>If the <code>Customer</code> would be a <em>required</em> navigation property in <code>Order</code> I could use the following projection and it works because I can be sure that a <code>Customer</code> always exists for any <code>Order</code>:</p>
  
  <pre><code>OrderViewModel viewModel = context.Orders
      .Where(o =&gt; o.Id == someOrderId)
      .Select(o =&gt; new OrderViewModel
      {
          ShippingRemark = o.ShippingRemark,
          CustomerViewModel = new CustomerViewModel
          {
              SalesLevel = o.Customer.SalesLevel,
              Name = o.Customer.Name
          }
      })
      .SingleOrDefault();
  </code></pre>
  
  <p>But this does not work when <code>Customer</code> is optional and the order with Id <code>someOrderId</code> does not have a customer:</p>
  
  <ul>
  <li><p>EF complains that the materialized value for <code>o.Customer.SalesLevel</code> is <code>NULL</code> and cannot be stored in the <code>int</code>, not nullable property <code>CustomerViewModel.SalesLevel</code>. That's not surprising and the problem could be solved by making <code>CustomerViewModel.SalesLevel</code> of type <code>int?</code> (or generally <em>all</em> properties nullable)</p></li>
  <li><p>But I would actually prefer that <code>OrderViewModel.CustomerViewModel</code> is materialized as <code>null</code> when the order has no customer.</p></li>
  </ul>
  
  <p>To achieve this I tried the following:</p>
  
  <pre><code>OrderViewModel viewModel = context.Orders
      .Where(o =&gt; o.Id == someOrderId)
      .Select(o =&gt; new OrderViewModel
      {
          ShippingRemark = o.ShippingRemark,
          CustomerViewModel = (o.Customer != null)
              ? new CustomerViewModel
                {
                    SalesLevel = o.Customer.SalesLevel,
                    Name = o.Customer.Name
                }
              : null
      })
      .SingleOrDefault();
  </code></pre>
  
  <p>But this throws the infamous LINQ to Entities exception:</p>
  
  <blockquote>
    <p>Unable to create a constant value of type 'CustomerViewModel'. Only
    primitive types (for instance ''Int32', 'String' und 'Guid'') are
    supported in this context.</p>
  </blockquote>
  
  <p>I guess that <code>: null</code> is the "constant value" for <code>CustomerViewModel</code> which is not allowed.</p>
  
  <p>Since assigning <code>null</code> does not seem to be allowed I tried to introduce a marker property in <code>CustomerViewModel</code>:</p>
  
  <pre><code>public class CustomerViewModel
  {
      public bool IsNull { get; set; }
      //...
  }
  </code></pre>
  
  <p>And then the projection:</p>
  
  <pre><code>OrderViewModel viewModel = context.Orders
      .Where(o =&gt; o.Id == someOrderId)
      .Select(o =&gt; new OrderViewModel
      {
          ShippingRemark = o.ShippingRemark,
          CustomerViewModel = (o.Customer != null)
              ? new CustomerViewModel
                {
                    IsNull = false,
                    SalesLevel = o.Customer.SalesLevel,
                    Name = o.Customer.Name
                }
              : new CustomerViewModel
                {
                    IsNull = true
                }
      })
      .SingleOrDefault();
  </code></pre>
  
  <p>This doesn't work either and throws the exception:</p>
  
  <blockquote>
    <p>The type 'CustomerViewModel' appears in two structurally incompatible
    initializations within a single LINQ to Entities query. A type can be
    initialized in two places in the same query, but only if the same
    properties are set in both places and those properties are set in the
    same order.</p>
  </blockquote>
  
  <p>The exception is clear enough how to fix the problem:</p>
  
  <pre><code>OrderViewModel viewModel = context.Orders
      .Where(o =&gt; o.Id == someOrderId)
      .Select(o =&gt; new OrderViewModel
      {
          ShippingRemark = o.ShippingRemark,
          CustomerViewModel = (o.Customer != null)
              ? new CustomerViewModel
                {
                    IsNull = false,
                    SalesLevel = o.Customer.SalesLevel,
                    Name = o.Customer.Name
                }
              : new CustomerViewModel
                {
                    IsNull = true,
                    SalesLevel = 0, // Dummy value
                    Name = null
                }
      })
      .SingleOrDefault();
  </code></pre>
  
  <p>This works but it's not a very nice workaround to fill all properties with dummy values or <code>null</code> explicitly.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>Is the last code snippet the only workaround, aside from making all properties of the <code>CustomerViewModel</code> nullable?</p></li>
  <li><p>Is it simply not possible to materialize an optional reference to <code>null</code> in a projection?</p></li>
  <li><p>Do you have an alternative idea how to deal with this situation?</p></li>
  </ol>
  
  <p>(I'm only setting the general <em>entity-framework</em> tag for this question because I guess this behaviour is not version specific, but I am not sure. I have tested the code snippets above with EF 4.2/<code>DbContext</code>/Code-First. Edit: Two more tags added.)</p>