score:1

Accepted answer

David Buchanan has posted a solution for this problem using reflection :

msdn forum

score:0

I'm not sure you can do this dynamically, but you can do it conditionally. Something like this:

switch(selected column name)
{
    case "student_no":
       q = q.where(p=>p.StudentNo == value);
       break;

    case "student_id":
       q = q.where(p=>p.StudentId == value);
       break;
}

You can iterate through your columns and keep building the wheres. The SQL won't be executed as long as none of the calls force the IQueryable to execute.

score:0

I think expression trees are the right way to do this, but I don't know them very well so I'm going to give you the alternate way I would have done this if I didn't feel like learning expression tree building..

public interface IFilter { IEnumerable RetreiveFilter(string filterValue); }

public class FirstNameFilter : IFilter
{
    private const string FILTER_TYPE_NAME = "First Name";
    public IEnumerable RetreiveFilter(string filterValue)
    {
        return _myData.Where(person => person.FirstName = filtervalue);
    }

    public override string ToString()
    {
        return FILTER_TYPE_NAME;
    }
}

Create a class like this for each filter type, and then fill your dropdown with these filters, and when they type info into the filter text, it will execute against the ((IFilter)filterDropDown.SelectedItem).RetreiverFilter(filterTextBox.Text);


Related Articles