Build a Lambda Expression Dynamically For Sort Acs/Desc

I have tried writing my own basic dynamic lambda expression for sorting the columns in grid panel. There are columns with different property type, and a few link to other object.

In this example Branch column (in computer object as a property) is link to Lookup object. I want to order by display field in Lookup object.

example of lambda expression:

db.Computers.OrderBy(x=>x.Location);

db.Computers.OrderBy(x=>x.Branch.display)

grid.JPG

Below code will generate the lambda expression dynamically to sort asc/desc.

public static IQueryable SortComputers(this IQueryable source, string Field, bool desc) {
      var elType = typeof(Computer);
      var fp = elType.GetProperty(Field);

      var method = desc ? "OrderByDescending" : "OrderBy";

      var ep = Expression.Parameter(elType, "x");      // x =>
      var member = Expression.MakeMemberAccess(ep, fp); // x.field
      Expression exprSelector;
      var tyLookup = typeof(Lookup);
      if (member.Type == tyLookup) {
        MemberInfo miDisplay = tyLookup.GetProperty("display");
        member = Expression.MakeMemberAccess(member, miDisplay); //x.field.display
      }
      exprSelector = Expression.Lambda(member, ep);    // x => x.field || x.field.display
      var exprResult = Expression.Call(typeof(Queryable), method, new Type[] { elType, member.Type },
                       source.Expression, Expression.Quote(exprSelector));

      return source.Provider.CreateQuery(exprResult);

    }

To call the function:

if (model.Sort.Length > 0) {
            results = mylinq.SortComputers(d, model.SimpleSort,
            (model.SimpleSortDirection == SortDirection.DESC)).ToArray();
          }