在EF上重复'where'子句

时间:2014-07-14 08:34:07

标签: c# linq lambda entity-framework-6.1

如何让EF'哪里有'条款重复如下:

var query = from p in db.Posts
            where p.Title == "title" && p.Author == "author"
            where p.Title == "title" && p.Author == "author"

            select p;

现在标题和作者当然会来自List对象,例如:

List<string> authors = new List<string>(){ "author1", "author2", "author3" }
List<string> titles = new List<string>(){ "title1", "title2", title3" }

我可以在这里使用Contains,但要求是相应地匹配Title和Author。自作者&amp; title是参数我不能只是对它们进行硬编码并进行简单的查询

示例SQL查询将是:

select * 
from Posts
where (title = "title1" AND author = "author1") OR
      (title = "title2" AND author = "author2") OR
      (title = "title3" AND author = "author3")

2 个答案:

答案 0 :(得分:1)

我假设你真的想要一个&amp;&amp;标题和作者之间,但||每个标题/作者组合之间。假设你有这些:

Expression<Func<Post, bool>> where1 = p => p.Title == "title1" && p.Author == "author1";
Expression<Func<Post, bool>> where2 = p => p.Title == "title2" && p.Author == "author2";

如果您创建以下扩展方法:

    /// <summary>
    /// Combines two lambda expressions into a single expression.
    /// In the returned expression, the parameter in the second expression will be replaced
    /// with the parameter from the first.
    /// </summary>
    /// <param name="source">The first expression to combine.</param>
    /// <param name="other">The second expression to combine.</param>
    /// <param name="combiner">
    /// How to combine the expression bodies.
    /// Example: <see cref="Expression.Or(System.Linq.Expressions.Expression,System.Linq.Expressions.Expression)"/>
    /// </param>
    /// <returns></returns>
    public static Expression<Func<T1, T2>> Combine<T1, T2>(
        this Expression<Func<T1, T2>> source,
        Expression<Func<T1, T2>> other,
        Func<Expression, Expression, BinaryExpression> combiner)
    {
        var sourceParam = source.Parameters[0];
        var visitor = new ParameterReplacerVisitor(other.Parameters[0], sourceParam);
        var visitedOther = visitor.VisitAndConvert(other, "Combine");
        Require.That(visitedOther != null, () => "VisitAndConvert failed to return a value.");
        var newBody = combiner(source.Body, visitedOther.Body);
        return Expression.Lambda<Func<T1, T2>>(newBody, sourceParam);
    }

...使用以下ParameterReplacerVisitor类:

/// <summary>
/// This class replaces one parameter with another everywhere in a given expression tree.
/// This is handy when you have two lambda expressions that you want to combine into one.
/// </summary>
public class ParameterReplacerVisitor : System.Linq.Expressions.ExpressionVisitor
{
    private readonly ParameterExpression _originalParameter;
    private readonly ParameterExpression _newParameter;

    public ParameterReplacerVisitor(ParameterExpression originalParameter, ParameterExpression newParameter)
    {
        _originalParameter = originalParameter;
        _newParameter = newParameter;
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        if (node == _originalParameter)
        {
            node = _newParameter;
        }
        return base.VisitParameter(node);
    }
}

...然后你可以像这样组合这些表达式:

var either = where1.Combine(where2, Expression.Or);

然后你可以说:

var query = db.Posts.Where(either);

答案 1 :(得分:0)

您的要求的第一印象是您可能需要执行.Contains从数据库中提取记录,然后使用Dynamic Linq进一步过滤掉您的参数。