سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

your can use dynamic linq. here is a example:

var result = northwind.Products
     .Where("CategoryID = 3 AND UnitPrice > 3")
     .OrderBy("SupplierID");

===

private object bindingSourceTbl1_DataSourceBinding(object sender, EventArgs e,Expression whereClause)
{
    using (SampleDbEntities dbo = new SampleDbEntities())
    {
         var q = from x in dbo.Tbl1
             where x.Id == (int)comboBoxPerson.SelectedValue && whereClause
             select x);
         q = q.Where(whereClause)// your must reference dynamic linq library.
                                 //whereClause is expression
               .Take(100);
         return q.ToList();

    }

}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top