重复& # 39;在# 39;EF条款

本文关键字:条款 EF 重复 | 更新日期: 2023-09-27 18:12:59

如何使EF 'where'子句重复如下:

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。自作者&title是参数,我不能硬编码它们然后做一个简单的查询

示例SQL查询将是:

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

重复& # 39;在# 39;EF条款

我假设你实际上想要一个&&在标题和作者之间,但在每个标题/作者组合之间。假设你有这些:

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);

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