无法在EF上翻译LINQ In子句

时间:2015-12-01 14:48:12

标签: c# entity-framework linq

我需要使用EF实体将这个简单的SQL查询转换为LINQ:

  

SELECT * FROM Clientes WHERE ID_Cliente IN(SELECT ID_Objeto FROM   Direcciones,其中ID_TipoDireccion = IDTipoDireccion)

似乎很简单,但似乎很难实现。我试试这个:

public List<Clientes> EnumEntity(int IDTipoDireccion)
{
    var dir = new DireccionesRepository(ref rep.Context);
    var misClientes = rep.ListEntity();

    var misDirColection = dir.ListEntity().ToList().Where(o => o.ID_TipoDireccion == IDTipoDireccion);

    foreach (var item in misDirColection)
    {        
        misClientes=misClientes.Where(p => p.ID_Cliente == item.ID_Objeto);
    }
    return misClientes.ToList();
}

上述查询的问题是使用AND。我需要它来使用OR来包含与Direcciones对象匹配的所有客户端。 我尝试PredicateBuilder类,但它不支持EF。我发现this adaptation of PredicateBuilder似乎解决了这个问题:

internal class SubstExpressionVisitor : System.Linq.Expressions.ExpressionVisitor
{
    public Dictionary<Expression, Expression> subst = new Dictionary<Expression, Expression>();

    protected override Expression VisitParameter(ParameterExpression node)
    {
        Expression newValue;
        if (subst.TryGetValue(node, out newValue))
        {
            return newValue;
        }
        return node;
    }
}

public static class PredicateBuilder
{
    /*public static Expression<Func<T,bool>> True<T>() { return f => true; }*/
    //public static Expression<Func<T, bool>> False<T>() { return f => false; }       

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> a, Expression<Func<T, bool>> b)
    {
        ParameterExpression p = a.Parameters[0];

        SubstExpressionVisitor visitor = new SubstExpressionVisitor();
        visitor.subst[b.Parameters[0]] = p;
        Expression body = Expression.AndAlso(a.Body, visitor.Visit(b.Body));
        return Expression.Lambda<Func<T, bool>>(body, p);            
    }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> a, Expression<Func<T, bool>> b)
    {
        ParameterExpression p = a.Parameters[0];

        SubstExpressionVisitor visitor = new SubstExpressionVisitor();
        visitor.subst[b.Parameters[0]] = p;
        Expression body = Expression.OrElse(a.Body, visitor.Visit(b.Body));
        return Expression.Lambda<Func<T, bool>>(body, p);
    }

}

但问题是我不知道如何在我的情况下使用它。 有谁能够帮我?

谢谢!

修改

在调整建议的代码后,这是其他人需要它的结果:

public List<EnumeradorWCFModel> EnumEntity(int IDTipoDireccion)
    {
        // SELECT * FROM Clientes WHERE ID_Cliente IN(SELECT ID_Objeto FROM Direcciones where ID_TipoDireccion=IDTipoDireccion)
        // Get All directions of type IDTipoDireccion
        var dir = new DireccionesRepository(ref rep.Context);

        var misDirColection = dir.ListEntity().Where(x => x.ID_TipoDireccion == IDTipoDireccion)
                .Select(x => x.ID_Objeto);  // Do not iterate!

        // Itinerate Clients
        var misClientes = rep.ListEntity().Where(x => misDirColection.Contains(x.ID_Cliente)).ToList();                

        return misClientes.ToList();
}

1 个答案:

答案 0 :(得分:2)

使用Contains()

SELECT * FROM Clientes WHERE ID_Cliente IN(SELECT ID_Objeto FROM Direcciones where ID_TipoDireccion=IDTipoDireccion)

可直接翻译为:

int IDTipoDireccion = ...

// First, create a query for your subselect.
var direcciones = dbContext.Direcciones
    .Where(x => x.ID_TipoDireccion == IDTipoDireccion);
    .Select(x => x.ID_Objeto);  // Do not iterate!

// Then, use the first query in the WHERE of your second query.
var results = dbContext.Clientes
    .Where(x => direcciones.Contains(x.ID_Cliente))
    .ToList();