我有一个基于Table Per Hierarchy继承的LINQ to Entities模型。我在基类型上进行了查询,想要执行特定类型相关的逻辑操作。例如:
IQueryable<BaseType> base = ...
// this works fine
var result = base.Select(b => b is DerivedType1 ? 1 : 2).ToList();
// this doesn't compile to SQL
var result2 = base.Select(b => b is DerivedType1 ? ((DerivedType1)b).DerivedProperty : null).ToList();
有没有办法在不分别处理每个派生类型的IQueryable的情况下完成类似于这样的操作:
// I'd rather not do this:
var resultA = base.OfType<DerivedType1>().Select(d => d.DerivedProperty);
var resultB = base.OfType<DerivedType2>().Select(d => default(int?));
var result = resultA.Concat(resultB).ToList();