0

I would like to do the following;

    public IEnumerable<SalesRegister> GetSalesRegister()
    {
        return _client.GetAllSalesRegisters().OrderBy(x => x.CompanyName);
    }

by doing something like;

    public IEnumerable<SalesRegister> GetSalesRegister(string sortBy)
    {
        return _client.GetAllSalesRegisters().OrderBy(x => sortBy);
    }

for this I have tried the following, but didn't work

    public IEnumerable<SalesRegister> GetSalesRegister(string sortBy)
    {
        var type = typeof(SalesRegister);
        var param = Expression.Parameter(type, "x");
        var len = Expression.PropertyOrField(param, sortBy);
        return _client.GetAllSalesRegisters().OrderBy(x => len );
    }

I am not sure if I can do it in this way, can somebody help me please?

1 Answer 1

3

Try this -

public IEnumerable<SalesRegister> GetSalesRegister(Expression<Func<SalesRegister, object>> sortByExp)
{
    return _client.GetAllSalesRegisters().OrderBy(sortByExp.Compile());
}

You can call it like this -

GetSalesRegister(x => x.CompanyName);

To pass a lambda expression you need to have the type as Expression<Func<T, TR>> where T is base class and TR is the return type of the expression

Or, if you only have the string name try building the expression like this -

public IEnumerable<SalesRegister> GetSalesRegister(string sortBy)
{
    var param = Expression.Parameter(typeof (SalesRegister), "x");
    var prop = typeof (SalesRegister).GetProperty(sortBy);
    var objectFuncType = typeof (Func<,>).MakeGenericType(typeof (SalesRegister), prop.PropertyType);
    var propExp = Expression.PropertyOrField(param, sortBy);
    var exp = Expression.Lambda(objectFuncType, propExp, param);
    var converted = Expression.Convert(exp.Body, typeof (object)); // only needed if you are passing in no referene types like int, double, etc as parameters, otherwise ignore this line and use 'exp' in place of 'converted' in the next line
    var sortByExp= Expression.Lambda<Func<SalesRegister, object>>(converted, exp.Parameters);

    return _client.GetAllSalesRegisters().OrderBy(sortByExp.Compile());
}

You can call it like this -

GetSalesRegister("CompanyName");
Sign up to request clarification or add additional context in comments.

5 Comments

what is TDomain here?
That is a typo, this is a copied code from one of my implementations. Missed to replace with the actual type. use SalesRegister for your implementation.
I am getting compile time error now - The type arguments for method 'IOrderedEnumerable<TSource> System.Linq.Enumerable.OrderBy<Tsource,TKey(this IEnumerable<TSource>,Func<TSource,TKey>)>' cannot be inferred from the usage. Try specifying the type arguments explicitly. in the last line of the GetSalesRegister Method.
Add the type argument for the OrderBy like this - OrderBy<SalesRegister>(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.