title: Create null enumerable within Linq to Entities query
body: <p>I am working on an ASP.NET MVC project which uses Entity Framework.</p>
  
  <p>I need to project values pulled from the database into a <code>PropertyValue</code> type, which looks like the following:</p>
  
  <pre><code>public class PropertyValue {
      public string StringValue { get; set; }
      public bool? BoolValue { get; set; }
      public int? IntValue { get; set; }
  }
  </code></pre>
  
  <p>Most of the time, this is easy enough. To filter all users with "First Name" = "John" and "Is Archived" = true, I could do:</p>
  
  <pre><code>usersQuery
      .Where(u =&gt; 
          new PropertyValue {
              StringValue = u.FirstName,
              BoolValue = null,
              IntValue = null
          }.StringValue == "John")
      .Where(u =&gt;
          new PropertyValue {
              StringValue = null,
              BoolValue = u.IsArchived,
              IntValue = null
          }.BoolValue == true);
  </code></pre>
  
  <p>Obviously this is a ridiculous looking query, but I'm constructing these queries piece by piece based on user input. These queries need to be combinable, which is why I have to explicitly set all the unused properties of <code>PropertyValue</code> to null, and I have to set them all in the same order. If I don't do this, I'll get an error from Entity Framework saying that PropertyValue exists in two structurally incompatible initializations within the query.</p>
  
  <blockquote>
    <p>System.NotSupportedException: The type 'UMS.Utilities.PropertyValue' 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>This isn't a problem, I can just make sure all the properties are explicitly set to null. The problem arises when I have to add another property to my <code>PropertyValue</code> type, to be able to retrieve a list of IDs (in my case, retrieving all selected roles for the user, i.e. "Admin", "Guardian", "Teacher", "Student")</p>
  
  <p>I've modified my <code>PropertyValue</code> class to be able to store a list of Guids:</p>
  
  <pre><code>public class PropertyValue {
      public string StringValue { get; set; }
      public bool? BoolValue { get; set; }
      public int? IntValue { get; set; }
      public IEnumerable&lt;Guid&gt; GuidValues { get; set; }
  }
  </code></pre>
  
  <p>I can now query for user roles like so:</p>
  
  <pre><code>usersQuery
      .Select(u =&gt; new PropertyValue {
          StringValue = null,
          BoolValue = null,
          IntValue = null,
          GuidValues = u.UserRoles.Select(ur =&gt; ur.Role_ID)
      });
  </code></pre>
  
  <p>That works great. Pulling first names would now look like this:</p>
  
  <pre><code>usersQuery
      .Select(u =&gt; new PropertyValue {
          StringValue = u.FirstName,
          BoolValue = null,
          IntValue = null,
          GuidValues = null
      });
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <blockquote>
    <p>System.NotSupportedException: Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only entity types, enumeration types or primitive types are supported in this context.</p>
  </blockquote>
  
  <p>So I can't create a null enumerable within EF. But I can't just ignore the initialization of the property, or else I get the structurally incompatible error. I've tried doing <code>GuidValues = Enumerable.Empty&lt;Guid&gt;()</code> instead, but I can't do that in an EF context either.</p>
  
  <p>How can I get around this issue and create a null or empty enumerable within an EF projection?</p>