2

嗨,我在 c# 中创建了一个 winform 应用程序。

我使用 EF5 处理数据库。

为了将数据绑定到我的 datagridviews,我从 BindingSource 创建了一个组件,该组件具有运行此事件的 Bind() 方法:

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

因为我的数据库有很多大数据,所以我获取了部分数据。我使用搜索来获取匹配记录。为此,我创建了一个 SearchPanel 组件,该组件创建用于过滤网格中每一列的文本框。

现在我想将表达式树作为参数发送到我的事件,以加入 where 子句,如下所示:

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

但是我的表达式树构建器方法存在于我的组件代码中,我无法访问我的项目中的 DbContext,只是我的组件中有 fieldNames,我也只想为所有表编写一种方法。

这意味着我不能像

表达式< Func< AnyDbSet,bool>>

我不知道怎么做?

谢谢

4

3 回答 3

7

如果您只需要&&,那么意识到coll.Where(x => a(x) && b(x))(在哪里a(x)b(x)是任何与 一起使用的布尔表达式x)在逻辑上与 coll.Where(x => a(x)).Where(x => b(x)). 这意味着您可以将代码重写为:

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

如果您还需要支持||或更复杂的组合,您可以使用LINQKit

这只是留下了从属性名称创建表达式的问题。您可以使用该Expression类型的方法。例如,类似:

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);
}
于 2013-11-14T14:49:54.280 回答
0

您可以使用动态 linq。这是一个例子:

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

    }

}

于 2013-11-14T06:14:30.033 回答
0

我建议使用 Joseph Albahari 的 PredicateBuilder。您可以在http://www.albahari.com/nutshell/predicatebuilder.aspx找到它。

于 2013-11-14T10:00:00.133 回答