1

I would like to build a LINQ query from string parameters. I have a grid in Silverlight and I want to filter a result of query by columns.

When the users click on a button on one of the grid columns a listview will be displayed. They select one or more value in the listview and after click on the filter button. In the code I get the selected values and the column member which is the property of the object that I binded to the grid.

For example I have an employee object which has a FullName Property. So I get the FullName property as a string and the selected values from the filter.

I would like to build a LINQ query from these information.

I can do it:

protected Expression<Func<T, bool>> CreateGetExpression<T>(int id)
    {
        ParameterExpression e = Expression.Parameter(typeof(T), "e");
        PropertyInfo propInfo = typeof(T).GetProperty(KeyPropertyName);
        MemberExpression m = Expression.MakeMemberAccess(e, propInfo);
        ConstantExpression c = Expression.Constant(id, typeof(int));
        BinaryExpression b = Expression.Equal(m, c);
        Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(b, e);
        return lambda;
    }

My problem is when I have a complex property. For example I have a columns which contains the Department name where the employees are working.

The query in LINQ looks like this Where(e => e.Department.Name == departmentName)

But I dont have any idea how I can build this query from string parameters.

Anybody can help me?

user295541
  • 1,015
  • 4
  • 16
  • 26
  • 1
    possible duplicate of [Expression tree for a member access of depth > 1](http://stackoverflow.com/questions/11167989/expression-tree-for-a-member-access-of-depth-1) – Jon Skeet Jun 24 '12 at 08:23

0 Answers0