0

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

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

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

Thanks.

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.

Here's what I have now.

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();
    }
Gabe N.
  • 11
  • 7

2 Answers2

1

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

  • 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
Community
  • 1
  • 1
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

Are you trying to call the method named in this.UniqueName on each employee, and see if the result contains the text? Is so, you can use reflection.

If you're certain that o is a RadComboBox, it might as well be passed in as such.

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

    PropertyInfo property = typeof(EmployeeDTO).GetProperty(this.UniqueName);
    var employees = from emp in EmployeeBL.GetAllEmployees()
                    where ((IQueryable<string>)(property.GetValue(emp))).Contains(e.Text)
                    select emp;
    o.DataSource = employees;

    o.DataBind();
}
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
  • I'm assuming `this.UniqueName` references a property name since the OP states it's a column in the grid. – D Stanley Feb 05 '13 at 22:14
  • Thanks @DStanley. I've changed it to a property and added the missing cast. – Matthew Strawbridge Feb 05 '13 at 22:21
  • I've never used Reflection... but I did try your suggestion, and Intellisense is saying that `property` doesn't have a GetValue method. I'm basically trying to load the list of employees into the combo box (configured as a filter value list) and when the user begins to type, the value is evaluated in the `where` clause. My return from `GetAllEmployees()` is a List. Not sure if that's affecting the `PropertyInfo` conflict. Thx. – Gabe N. Feb 06 '13 at 17:03
  • The `Property` type does have a `GetValue` method. The error is probably saying that the actual property being called doesn't exist. This code expects there to be a property in `EmployeeDTO` of type `IQueryable`, the name of which is specified in your `UniqueName`. – Matthew Strawbridge Feb 06 '13 at 23:03
  • @MatthewStrawbridge I'm totally new to this, but I really want to understand what you're suggesting, b/c I think I'm close. If you could, can you walk me through what's going on in your example? I think, as you suggest, there's a conflict between the `Property` lookup on `EmployeeDTO` and what's actually being matched in the column values. The match that I'm hoping to make is between the values in the column (for example, **FullName** is both a `Property` of the `EmployeeDTO` and a `UniqueName` of a GridColumn `this`) and the text (`e.Text`) being typed into the `ComboBox`. – Gabe N. Feb 07 '13 at 14:51
  • @GabeN. FWIW, here's the [dummy code](http://pastebin.com/thBwW9SD) I knocked up when looking into this. I'm not sure how much use it will be to you. You may be better off finding some example code that's close to what you need. – Matthew Strawbridge Feb 08 '13 at 20:53