我的泛型类中有以下方法:
// 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(); } }
表达式visitor实现如下:
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); } }
原文地址:https://www.jb51.cc/csharp/244362.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。