0

Intro

Let's say that I have these classes:

public class Door {
    public string Label { get; set; }
}

public class House {
    public string Address { get; set; }
    public Door FrontDoor { get; set; }
}

Now, those are simple models in the DB, abstracted using Entity Framework 4. Therefore, when I need to make an ordered list of House then I can use:

dbObjectContext.Houses.OrderBy(h=>h.Name);

Then, if I want to order by FrontDoor.Label, I can use:

dbObjectContext.Houses.OrderBy(h=>h.FrontDoor.Label);

Problem

I intend to make a sorting method that is intended to be used by a WPF DataGrid that will display a list of Houses. The method signature is like below:

IOrderedQueryable<TEntity> ApplySortingToQuery<TEntity>(bool ascending
    ,IQueryable<TEntity> finderQuery,string propertyPath)

I want to build OrderBy expression using the knowledge of the object type TEntity and the property access path string propertyPath.

In the DataGrid, the objects to be sorted is of type House, and it's columns are Address and Front Door Label. Now, the Address column in the DataGrid has SortMemberPath set to Address. Whereas, the Front Door Label column's is FrontDoor.Label.

In the beginning, I can just use if-else to determine what the OrderBy expression to be used, e.g.:

if (col.SortMemberPath=="Address") {
    query=query.OrderBy(h=>h.Address);
} else if (col.SortMemberPath=="FrontDoor.Label") {
    query=query.OrderBy(h=>h.FrontDoor.Label);
}

However, this functionality is very generic IMHO. So, a utility function to do it that will consult SortMemberPath and use it as propertyPath in the method is going be a great help for many other applications that will use it.

I've consulted this technique that help me to somewhat make this utility function, but only direct property access is successful (i.e.: Address), but accessing sub property (i.e.: FrontDoor.Label) isn't successful.

Please guide me.

Community
  • 1
  • 1
Roland
  • 19
  • 2
  • see this: [`Access nested properties with dynamic lambda using Linq.Expression`](http://stackoverflow.com/questions/1674041/access-nested-properties-with-dynamic-lambda-using-linq-expression) – user1416420 Feb 15 '13 at 07:47
  • or: [How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?](http://stackoverflow.com/questions/307512/how-do-i-apply-orderby-on-an-iqueryable-using-a-string-column-name-within-a-gene) – Corey Feb 15 '13 at 08:06
  • Hmm... 'dynamic query' is the keywords... I'll check and test it first. Thanks. – Roland Feb 15 '13 at 09:27

1 Answers1

0

The technique is in the web all along.. (as always)

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Now, I just need to compile a DLL using CSharpSamples.zip\LinqSamples\DynamicQuery\DynamicQuery\Dynamic.cs, making the System.Linq.Dynamic namespace available. Then, the method implementation will become very simple.

The important keywords are dynamic query... The right keywords for the right question sometimes are hard to come by.

Roland
  • 19
  • 2