3

I have a question, how do I add another filter and this I have to validate that was selected?

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

Update: I'll use the expression in this function:

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

What I was trying to do is this but with an Expression

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

    }
}
davidterra
  • 301
  • 3
  • 15

5 Answers5

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 ...
  ;
}
Nick Butler
  • 24,045
  • 4
  • 49
  • 70
0

Okay, well here is a sample on how to embed lambda statements. It's not your example, but here ya go:

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);
corylulu
  • 3,449
  • 1
  • 19
  • 35
0

It is simple.Visit this link

the tricky thing is invoking the OrderByAlias - using MakeGenericMethod may be the way, as shown in the link above.

Community
  • 1
  • 1
Rahul Ranjan
  • 1,028
  • 1
  • 10
  • 27
0

Try this way:

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

inherit from 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);
    }
}

your Filter() method might look like this

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

example

have a look at the following for more information

How to: Modify Expression Trees (C# and Visual Basic)