title: Filtering OData request with inherited entities causes casting exception
body: <p>I feel like I'm asking a lot of questions, but I keep getting stuck.  I am developing an OData service, and I want an entity that can have several user-designated name-value pairs associated, which can then be searched against.  I am using EF4.3, DataServiceVersion 3.0.  I am using a custom Metadata, Query, and Update provider.</p>
  
  <p>So let's say that I have an entity Person:</p>
  
  <pre><code>public class Person : EntityBase
  {
      public virtual IList&lt;Property&gt; PropertySet { get; set; }
  }
  </code></pre>
  
  <p>(EntityBase is a common POCO that all my entities come from; it has only a Guid ID property.)  Now let's define our Property:</p>
  
  <pre><code>public abstract class Property : EntityBase
  {
      public string Name { get; set; }
      public Person Person { get; set; }
      public Guid PersonId { get; set; }
  }
  
  public class IntProperty : Property
  {
      public int? IValue { get; set; }
  }
  
  public class StringProperty : Property
  {
      public string SValue { get; set; }
  }
  </code></pre>
  
  <p>So far, so good.  In my configuration, I am using Table Per Hierarchy for inheritance.</p>
  
  <p>Now, I am able to add a Property to my Person, and when I make a request like this:</p>
  
  <pre><code>GET /Service/People(guid'THE_ID')?$expand=PropertySet
  </code></pre>
  
  <p>It works:</p>
  
  <pre><code>{"d": {
    "__metadata": {...},
    "PropertySet": {
      "results": [{
        "__metadata": {...},
        "Id": "PROP_1_ID",
        "Name": "Number",
        "IValue": 1234
      },{
        "__metadata": {...},
        "Id": "PROP_2_ID",
        "Name": "EmailAddress",
        "SValue": "AAAA"
      }]
    },
    "Id": "THE_ID",
    }
  }
  </code></pre>
  
  <p>If I query for a Person that has a property named 'EmailAddress', that works:</p>
  
  <pre><code>GET /Service/People?$expand=PropertySet&amp;$filter=PropertySet/any(x: x/Name eq 'EmailAddress')
  </code></pre>
  
  <p>But even for that, I had to pull a few tricks.  I implemented an expression visitor, and whacked out a few comparisons that Linq To Entities didn't seem to like:</p>
  
  <pre><code>protected override Expression VisitBinary(BinaryExpression node)
  {
    if (node.NodeType == ExpressionType.Equal)
    {
      Expression left = Visit(node.Left);
      Expression right = Visit(node.Right);
  
      ConstantExpression rightConstant = right as ConstantExpression;
      if (null != rightConstant &amp;&amp; rightConstant.Value == null)
      {
        if (left.Type == typeof(IList&lt;Property&gt;))
        {
          return Expression.Constant(false, typeof(bool));
        }
      }
    }
    return base.VisitBinary(node);
  }
  
  protected override Expression VisitConditional(ConditionalExpression node)
  {
    Expression visitedTest = Visit(node.Test);
    Expression visitedIfTrue = Visit(node.IfTrue);
    Expression visitedIfFalse = Visit(node.IfFalse);
    ConstantExpression constantTest = visitedTest as ConstantExpression;
    if (null != constantTest &amp;&amp; constantTest.Value is bool)
    {
      return ((bool)constantTest.Value) ? visitedIfTrue : visitedIfFalse;
    }
    return Expression.Condition(visitedTest, visitedIfTrue, visitedIfFalse);
  }
  </code></pre>
  
  <p>The gist is with the first override my query gets expressions such as "it.PropertySet == null", which I know will always be untrue.  (In my spike, the only thing that has PropertySet is a Person, and a Person always has a PropertySet.)  In the second override, I'm looking at expressions such as "IIF((it.PropertySet == null), Empty(), it.PropertySet)", and I know that "it" will always have a PropertySet.  This prevents errors comparing an IList against null.</p>
  
  <p>Now, the problem.</p>
  
  <p>Merely searching for the presence of a Property isn't enough.  I would like to check it's value:</p>
  
  <pre><code>GET /Service/People?$expand=PropertySet&amp;$filter=PropertySet/any(x: x/Name eq 'EmailAddress' and cast(x, 'InheritedPropertyTest.Entities.StringProperty')/SValue eq 'AAAA')
  </code></pre>
  
  <p>And this is the resulting query:</p>
  
  <pre><code>value(System.Data.Objects.ObjectQuery`1[InheritedPropertyTest.Entities.Person])
  .MergeAs(AppendOnly)
  .Where(it =&gt; it.PropertySet.Any(x =&gt; ((x.Name == "EmailAddress") AndAlso (IIF((Convert(x) == null), null, Convert(x).SValue) == "AAAA"))))
  .OrderBy(p =&gt; p.Id)
  .Take(100)
  .Select(p =&gt; new ExpandedWrapper`2() {ExpandedElement = p, Description = "PropertySet", ReferenceDescription = "", ProjectedProperty0 = p.PropertySet.OrderBy(p =&gt; p.Id)    .Take(100)})
  </code></pre>
  
  <p>But I get this error: "Unable to cast the type 'InheritedPropertyTest.Entities.Property' to type 'InheritedPropertyTest.Entities.StringProperty'. LINQ to Entities only supports casting Entity Data Model primitive types."  So... now I'm stuck beating my head against the wall again.  Perhaps my inheritance isn't set up correctly?  Do I need to overload some other Expression Visitor method to make the Convert work?  How do I convince Linq To Entities to work with inherited properties?</p>
  
  <p>Thanks!</p>