score:21

Accepted answer

you can use reflection if you are using linq to objects, for instance you can use this:

_customerinterface.customerinterface.getallcustomers()
     .groupby(c => c.gettype().getproperty(category).getvalue(c, null))

if you are using linq to sql then an alternative is to use dynamic queries, check this link

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

score:3

you may build expression dynamically:

    expression<func<customer,t>> buildexpression<t>(string category)
    {
        //first build parameter of lambda ( 'c =>' part of expression)
        parameterexpression param = expression.parameter(typeof(customer), "c");
        //then body of expression : ' => c.category'
        expression<func<customer, t>> exp = expression.lambda<func<customer, t>>(expression.property(param, category), param);
        return exp;
    }

and finally, call

_customerinterface.customerinterface.getallcustomers()
    .groupby(buildexpression(category))

edit: well, sorry you still have to know the type of property to give t type parameter to buildexpression function

there are ways to do this, using for example getproperty(category).propertytype and then call makegenericmethod on gettype().getmethod("buildexpression<>"), but this requires a little more work. anyway, you'll have to find a way to build customerreportsummaryviewmodel from this type.

i don't know your use case, but you maybe all categories properties are of the same type, so you could hard-code it ?

if you are interested, and can't find a proper way to do it let me know i'll try to write a proper solution.


Related Query

More Query from same tag