在linq where子句中评估列名 [英] evaluate column name in linq where clause

查看:77
本文介绍了在linq where子句中评估列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在这里遗漏了一些明显的东西,我表示歉意.

My apologies if I'm missing something obvious here....

我正在尝试自定义一种方法来创建RadComboBox过滤器,该过滤器可以根据用户类型进行调整(基于Telerik演示).我使用业务逻辑层提取数据源,然后尝试使用linq来选择组合框OnItemsRequested的值,具体取决于哪个组合框发出了请求.我试图根据哪个GridColumn过滤器发出请求,在"where"子句中设置参数.

I'm trying to customize a method to create a RadComboBox filter that adjusts as a user types (based on a Telerik demo). I'm using a Business Logic layer to pull in my datasource, and then I'm trying to use linq to select the values for the combo box OnItemsRequested depending on which combo box has made the request. I'm trying to have set the parameters in the "where" clause based on which GridColumn filter is making the request.

这是我的代码来填充列表:

Here's my code to fill the list:

private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        ((RadComboBox)o).DataTextField = this.DataField;
        ((RadComboBox)o).DataValueField = this.DataField;
        var employees = from emp in EmployeeBL.GetAllEmployees()
                        where emp.(this.UniqueName).Contains(e.Text)
                        select emp;
        ((RadComboBox)o).DataSource = employees;
        ((RadComboBox)o).DataBind();
    }

我是否需要在数据对象(EmployeeDTO)中将UniqueName强制转换为参数?

Do I need to cast the UniqueName as parameter in my Data Object (EmployeeDTO)?

谢谢.

更新:: 多亏了反馈,我在填充组合框列表方面取得了一些成功.但是,我认为我的linq语句仍然有误导之处.该列表是第一次构建的,但是,当我尝试进行"StartsWith"比较时,即使我肯定键入了"findable 字符串.

UPDATE:: Thanks to feedback, I've had some success with populating the combobox list. However, I think I've still got a miscue in my linq statement. The list builds the first time around, however, when I try to do the "StartsWith" comparison, the page throws an error saying the datasource contains no datarows, even though I'm definitely typing a "findable" string.

这就是我现在拥有的.

private void list_ItemsRequested(RadComboBox o, RadComboBoxItemsRequestedEventArgs e)
    {
        o.DataTextField = this.DataField;
        o.DataValueField = this.DataField;

        DataTable dt = EmployeeBL.GetAllEmployees().AsDataTable();

        IEnumerable<DataRow> query =
            from emp in dt.AsEnumerable()
            where emp.Field<String>(this.UniqueName).StartsWith(e.Text)
            select emp;

        DataTable boundTable = query.CopyToDataTable<DataRow>();
        o.DataSource = boundTable;
        o.DataBind();
    }

推荐答案

没有内置方法.您可以选择:

There's not a built-in way. You have some choices:

  • 使用动态Linq查询库,例如构建表达式属性名称
  • 使用switch语句从已知的属性列表中选择表达式(易于编码,动态性较低)
  • 使用 CopyToDataTable 扩展方法来创建数据支持通过 DataView s
  • Use a Dynamic Linq query library like ScottGu's
  • Use reflection to build an Expression from the property name
  • Use a switch statement to select an expression from a known list of properties (easy to code, less dynamic)
  • Use the CopyToDataTable extension method to create a data table that does support string-based sorting/filtering through DataViews

这篇关于在linq where子句中评估列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆