繁体   English   中英

将 IQueryable 类型转换为 Linq to Entities 中的接口

[英]Cast a IQueryable type to interface in Linq to Entities

我的通用类中有以下方法:

// 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值的所有记录。 但是,我有这个错误:

无法将“WebTest.Models.Entities.Product”类型转换为“Data.IActivable”类型。 LINQ to Entities 仅支持转换 EDM 原语或枚举类型。

我明白为什么会发生这个错误。 但是关于 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 会正确地选择它。

诀窍是将整个 IQueryable<TEntity> 转换为 IQueryable<IActivable> 而不是第一次转换:

if (typeof(IActivable).IsAssignableFrom(typeof(TEntity)))
{
    return ((IQueryable<IActivable>)(this.repository.Get()))
        .Where(q => q.Active)
        .Cast<TEntity>();
}

目前我有一个替代方法是使用扩展方法。 然而缺点是我的 IBaseService 不能声明GetActive方法,因为具体的类实际上没有实现它。

public static class BaseServiceExtension
{

    public static IQueryable<TEntity> GetActive<TEntity, TKey>(this IBaseService<TEntity, TKey> service) 
        where TEntity : class, IEntity<TKey>, IActivable
    {
        return service.Get().Where(q => q.Active);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM