加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将一个IQueryable类型转换为Linq to Entities中的接口

发布时间:2020-12-15 08:39:11 所属栏目:百科 来源:网络整理
导读:我的泛型类中有以下方法: // This is the class declarationpublic abstract class BaseServiceTEntity,TKey : IBaseServiceTEntity,TKey where TEntity : class,IEntityTKey// The Methodpublic IQueryableTEntity GetActive(){ if (typeof(IActivable).Is
我的泛型类中有以下方法:
// 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);
  }
}

它只是检查是否需要强制转换:如果实际值已经实现了它所投射的类型,它只会从表达式中删除转换,EF将正确地获取它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读