How to write a generic expression tree for filter data in c#

c# entity-framework expression-trees

Question

Hi I Create a winform Application in c#.

I use EF5 to work with database.

and for bind data to my datagridviews i created a component from BindingSource that has a Bind() method that run this event:

private object bindingSourceTbl1_DataSourceBinding(object sender, EventArgs e)
{
    using (SampleDbEntities dbo = new SampleDbEntities())
    {
      return(from x in dbo.Tbl1
             where x.Id == (int)comboBoxPerson.SelectedValue
             select x).Take(1000).ToList();
    }
}

because my database has many large data i fetch partial of data. and i use search for get match record. for this i created a SearchPanel component that create textboxes for filter each columns in my grid.

at now i want to send an expression tree to my event as a parameter to join to where clause like this:

private object bindingSourceTbl1_DataSourceBinding(object sender, EventArgs e,Expression whereClause)
{
    using (SampleDbEntities dbo = new SampleDbEntities())
    {
      return(from x in dbo.Tbl1
             where x.Id == (int)comboBoxPerson.SelectedValue && whereClause
             select x).Take(1000).ToList();
    }
}

but my expression tree builder method exists in my component code and i dont access to my DbContext in my project and just i have fieldNames in my component, also i want to write just one method for all tables.

this means i can't return like

Expression< Func< AnyDbSet,bool>>

and i don't know how do it?

Thanks

Accepted Answer

If you need just &&, then it's helpful to realize that coll.Where(x => a(x) && b(x)) (where a(x) and b(x) are any boolean expressions that work with x) is logically the same as coll.Where(x => a(x)).Where(x => b(x)). What this means is that you can rewrite your code to something like:

List<Tbl1Type> GetTbl1Values(Expression<Func<Tbl1Type, bool>> whereClause)
{
    using (SampleDbEntities dbo = new SampleDbEntities())
    {
        return dbo.Tbl1
            .Where(x => x.Id == (int)comboBoxPerson.SelectedValue)
            .Where(whereClause)
            .Take(1000).ToList();
    }
}

If you also needed to support || or more complicated combinations, you could use LINQKit.

This just leaves the matter of creating the expression from a property name. You can use method of the Expression type for that. For example, something like:

static Expression<Func<T, bool>> CreateWhereClause<T>(
    string propertyName, object propertyValue)
{
    var parameter = Expression.Parameter(typeof(T));
    return Expression.Lambda<Func<T, bool>>(
        Expression.Equal(
            Expression.Property(parameter, propertyName),
            Expression.Constant(propertyValue)),
        parameter);
}

Popular Answer

I would recommend using the PredicateBuilder by Joseph Albahari. You can find it at http://www.albahari.com/nutshell/predicatebuilder.aspx.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why