我的泛型类中有以下方法:
// This is the class declaration
public abstract class BaseService<TEntity, TKey> : IBaseService<TEntity, TKey> where TEntity : class, IEntity<TKey>
// The Method
public IQueryable<TEntity> GetActive()
{
if (typeof(IActivable).IsAssignableFrom(typeof(TEntity)))
{
return this.repository.Get().Cast<IActivable>()
.Where(q => q.Active)
.Cast<TEntity>();
}
else
{
return this.Get();
}
}
这是界面:
public interface IActivable
{
bool Active { get; set; }
}
基本上,TEntity
是一个实体 (POCO) 类,如果它们具有 Active
属性,则可以实现 IActivable。我想要该方法返回所有具有 Active
值 true 的记录。但是,我有这个错误:
Unable to cast the type 'WebTest.Models.Entities.Product' to type 'Data.IActivable'. LINQ to Entities only supports casting EDM primitive or enumeration types.
我明白为什么会出现这个错误。但是关于 SO 的文章对我的情况没有任何有效的解决方案。是否可以通过 Cast
或任何其他方式实现?注意:我不想转换为IEnumerable
,我想保留IQueryable
。
最佳答案
EF 表达式解析器无需转换即可工作,但是如果不进行转换,您将无法编译 C# 代码(C# 会提示它不知道 TEntity
有一个 Active
属性)。解决方案是:针对 C# 编译器进行强制转换,而不是针对 EF 表达式解析器进行强制转换。
因此,如果您确定(您正在 if
中检查它,所以您是)该对象实现了 IActivable
,您可以使用强制转换创建表达式 (用于编译),然后在运行时删除 EF 的强制转换(这是不必要的)。对于您的特定情况:
public IQueryable<TEntity> GetActive()
{
if (typeof(IActivable).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> getActive = x => ((IActivable)x).Active;
getActive = (Expression<Func<TEntity, bool>>)RemoveCastsVisitor.Visit(getActive);
return this.repository.Get().Where(getActive);
}
else
{
return this.Get();
}
}
表达式访问者是这样实现的:
internal class RemoveCastsVisitor : ExpressionVisitor
{
private static readonly ExpressionVisitor Default = new RemoveCastsVisitor();
private RemoveCastsVisitor()
{
}
public new static Expression Visit(Expression node)
{
return Default.Visit(node);
}
protected override Expression VisitUnary(UnaryExpression node)
{
if (node.NodeType == ExpressionType.Convert
&& node.Type.IsAssignableFrom(node.Operand.Type))
{
return base.Visit(node.Operand);
}
return base.VisitUnary(node);
}
}
它只是检查是否需要强制转换:如果实际值已经实现了它要强制转换到的类型,它只会从表达式中删除转换,EF 会正确地选择它。
关于c# - 将 IQueryable 类型转换为 Linq to Entities 中的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647190/