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

8

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

// 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 的记录。但是,我遇到了这个错误:
无法将类型 'WebTest.Models.Entities.Product' 强制转换为类型 'Data.IActivable'。LINQ to Entities 仅支持向 EDM 基元或枚举类型进行强制转换。
我明白为什么会出现这个错误。但是 SO 上的文章没有任何适用于我的情况的有效解决方案。是否可以使用 Cast 或其他方式来实现?注意:我不想转换为 IEnumerable,我想保留 IQueryable
3个回答

12
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会正确地获取它。


1
不知道可以绕过类型转换,好的解决方案! - Alexander Derck
2
很棒的东西,尽管将接口命名为IActivable让我感到有点不舒服!请将其更改为IActivatable :) - Tom Deloford

5
关键在于将整个IQueryable<TEntity>强制转换为IQueryable<IActivable>,而不是第一个转换:
if (typeof(IActivable).IsAssignableFrom(typeof(TEntity)))
{
    return ((IQueryable<IActivable>)(this.repository.Get()))
        .Where(q => q.Active)
        .Cast<TEntity>();
}

好的,干净的解决方案! - Horosho
简单、干净、有效。在我看来,应该被接受为答案。 - Dehalion

0

目前我的一个替代方案是使用扩展方法。然而,缺点是我的 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);
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接