3

我有一个问题,如何添加另一个过滤器并且我必须验证它是否被选中?

private Expression < Func < Entity.Modelos.Flux, bool >> Filter() {
    var dateStart = dtpDateStart.Value.Date;
    var dateEnd = dtpDateEnd.Value.Date;

    Expression < Func < Entity.Modelos.Flux, bool >> expr = null;

    expr = f = > f.DatFlux >= dateStart.Date && f.DatFlux <= dateEnd.Date;

    if (txtDescription.Text != String.Empty) {
        //add filter
    }

    return expr;
}

更新:我将在此函数中使用表达式:

public virtual IQueryable < T > Filter(Expression < Func < T, bool >> expressao) {
    return DbSet.Where(expressao).AsQueryable < T > ();
}

我试图做的是这个,但有一个表达

public List < Users > GetUsers(int ? id, string name) {
    using(DBContext ctx = new DBContext()) {
        IQueryable query = ctx.Usuarios;
        if (id.HasValue)
            query = query.Where(x = > x.ID == id);

        if (!string.IsNullOrEmpty(name))
            query = query.Where(x = > x.Name.StartsWith(name));

        return query.ToList();

    }
}
4

5 回答 5

0

If this is just a demo of your requirements, then you can create and modify Expression Trees using the System.Linq.Expressions namespace.

However, in the case of your question, it would probably be easier to use EF:

bool filterDescription = !String.IsNullOrEmpty( txtDescription.Text );

expr = f =>
  (
    ( f.DatFlux >= dateStart.Date && f.DatFlux <= dateEnd.Date )
    &&
    ( !filterDescription || ... add filter ... )
  )
;

or normal C#:

if ( String.IsNullOrEmpty( txtDescription.Text ) )
{
  expr = f => f.DatFlux >= dateStart.Date && f.DatFlux <= dateEnd.Date;
}
else
{
  expr = f => f.DatFlux >= dateStart.Date && f.DatFlux <= dateEnd.Date
    &&
    ... add filter ...
  ;
}
于 2013-03-19T17:51:14.243 回答
0

好的,这里有一个关于如何嵌入 lambda 语句的示例。这不是你的例子,但你去吧:

Func<int, int, EventHandler> makeHandler =
    (dx, dy) => (sender, e) => {
        var btn = (Button) sender;
        btn.Top += dy;
        btn.Left += dx;
    };

btnUp.Click += makeHandler(0, -1);
btnDown.Click += makeHandler(0, 1);
btnLeft.Click += makeHandler(-1, 0);
btnRight.Click += makeHandler(1, 0);
于 2013-03-19T17:56:15.900 回答
0

这很简单。访问此链接

棘手的事情是调用OrderByAlias- usingMakeGenericMethod可能是方式,如上面的链接所示。

于 2013-03-19T17:59:29.050 回答
0

试试这个方法:

private Expression<Func<Entity.Modelos.Flux, bool>> Filter()
{
    var dateStart = dtpDateStart.Value.Date;
    var dateEnd = dtpDateEnd.Value.Date;

    Func<Entity.Modelos.Flux, bool> expr = null;

    expr = f => f.DatFlux >= dateStart.Date && f.DatFlux <= dateEnd.Date;

    if(txtDescription.Text != String.Empty)
    {
       expr = f => expr(f) && f.Title.Equals(txtDescription.Text); // ← Your additional filter
    }

    return f => expr(f);
}
于 2013-03-19T18:04:55.930 回答
0

继承自ExpressionVisitor

public class MyVisitor: ExpressionVisitor
{
    private LambdaExpression visitor;
    public Expression Modify(Expression expression, LambdaExpression visitor)
    {
        this.visitor = visitor;
        return Visit(expression);
    }

    protected override Expression VisitBinary(BinaryExpression b)
    {
        var binary = visitor.Body as BinaryExpression;

        return Expression.MakeBinary(ExpressionType.AndAlso, b, binary, b.IsLiftedToNull, b.Method);
    }
}

你的Filter()方法可能看起来像这样

    private Expression<Func<Entity.Modelos.Flux, bool>> Filter()
    {
        var dateStart = dtpDateStart.Value.Date;
        var dateEnd = dtpDateEnd.Value.Date;
        var description = txtDescription.Text;

        Expression<Func<Entity.Modelos.Flux, bool>> expr = null;

        expr = f => f.DatFlux >= dateStart.Date && f.DatFlux <= dateEnd.Date;

        if (description != String.Empty)
        {
            //add filter
            Expression<Func<Entity.Modelos.Flux, bool>> other = f => f.Description == description;

            var modifier = new MyVisitor();
            expr = (Expression<Func<Entity.Modelos.Flux, bool>>)modifier.Modify((Expression)expr, (LambdaExpression)other);
        }

        return expr;
    }

例子

查看以下内容以获取更多信息

如何:修改表达式树(C# 和 Visual Basic)

于 2013-03-19T20:57:21.853 回答