Passing a lambda expression as a parameter to a method?

It looks like this would be a general requirement, but I can't seem to find a solution anywhere.

I have a method that will OrderBy

collect depending on the parameter passed to it.

I would like to pass the content of "OrderBy" to a method, but I cannot figure out how.

What i tried

I tried a switch with a string (ie if you pass "Name" it will go to case

which orders it by name) but that seems "hacked" and unnecessary.

I know it's kind of like Func<TEntity, TResult>

, but I can't hack it.

PSEUDO CODE:

GetOrderedCollection([NOT SURE] orderBy)
{
  return collection.OrderBy(orderBy);
}

      

+3


source to share


5 answers


OrderBy is an extension method with the following signature:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
)

      

(source: https://msdn.microsoft.com/it-it/library/bb534966%28v=vs.110%29.aspx )



so your method needs Func as an argument, where TSource is the List type and TKey is the type returned by your lambda. An example would be:

public void Method<TSource, TKey>(List<TSource> list, Func<TSource, TKey> func)
        {
            ...
            IOrderedEnumerable<TSource> orderedEnumerable = list.OrderBy(func);
        }

      

EDIT: I also noticed that in your example code, you declared your method as void, bu, then you are trying to return IOrderedEnumerable. If you want to return an ordered collection, your method must at least be of type IEnumerable (but this can lead to an end to it, since IEnumerables does not guarantee order. A more likely version would be to return List<TSource>

and calllist.OrderBy(func).ToList()

+3


source


In its most general form, your method should look like this:

IOrderedEnumerable<T1> GetOrderedCollection<T1, T2>(IEnumerable<T1> collection, Func<T1, T2> orderBy)
{
    return collection.OrderBy(orderBy);
}

      



T1

is the type of items in the list, and T2

is the type of property T1

you want to order.

+1


source


Does this meet your requirements?

    static void Main(string[] args) {           
        var objects = new List<Test>();
        GetOrderedCollection(objects, x => x.Name);
    }

    class Test {
        public string Name { get; set; }
    }

    static IEnumerable<TEntity> GetOrderedCollection<TEntity>(IEnumerable<TEntity> objects, Func<TEntity, object> orderBy) {
        return objects.OrderBy(orderBy);
    }

      

0


source


Yes, you are looking Func<TEntity, TResult> orderingFunc

. What matters is what the output ( TResult

) is IComparable

, so that the function OrderBy

can sort your results correctly.

So, something like this will work well:

public void OrderByDynamic(Func<Model, object> orderByFunc)
{
    collection = collection.OrderBy(orderByFunc); //don't forget to assign the return value
}

      

You can call it with lambda functions as usual:

OrderByDynamic(m => m.SortObj); //as long as this property/return value is IComparable

      

.NET Fiddle demonstrates this idea

0


source


The parameter must be either Func

or Action

. For more information see the answer to this question .

-1


source







All Articles