【发布时间】:2011-08-25 05:05:41
【问题描述】:
所以我有一个具有导航属性的实体,该属性具有类层次结构的类型。 (实体名称更改以保护有罪者)
class ParentEntity
{
virtual ChildEntity TheProperty { get; set; }
virtual string AnotherProperty { get; set; }
virtual string AnotherProperty2 { get; set; }
}
class ChildEntity
{
}
class ChildSubEntity : ChildEntity
{
virtual string InterestingProperty { get; set; }
}
如何查询 ParentClass 实体,我的查询条件之一是 TheProperty 的类型为 ChildSubClass 并且 InterestingProperty 具有特定值?
我试过了
ObjectContext context = GetContext();
var result = context.ParentEntities.
Where(e => e.AnotherProperty == AnotherInterestingValue).
Where(e => e.TheProperty is ChildSubEntity).
Where(e => ((ChildSubEntity)e.TheProperty).
InterestingProperty == InterestingValue).
ToList();
并得到错误“无法将类型 'ChildEntity' 转换为类型 'ChildSubEntity'。LINQ to Entities 仅支持转换实体数据模型原始类型。”。
我不得不满足于扁平化列表,并在从实体存储中检索数据后应用此条件。是否可以将这个条件写成 LINQ to Entities 可以接受的形式?
明确地说,这是一种简化,我实际上以编程方式应用了许多条件,使用LinqKit 构建查询表达式,其中一些条件针对 ParentEntity 的属性,一些针对 ParentEntity,还有一些针对ParentEntity 的其他子实体。
【问题讨论】:
标签: c# entity-framework inheritance entity-framework-4 linq-to-entities