Create Func dynamically

c#-7.0 dynamic-linq func


Related Subjects

Create Expression<Func<T, TKey>> dynamically

I looked online, but every example had an explanation.Expression<Func< How can I construct a page dynamically?Func<T, TKey> from T ?


Edit 1)

The T enter my code for instance, I want to sort my list using in runtimeName . Now, how can I do this?Func<T, TKey> = o=>o.Name;

Edit 2)

Please think about this:

public abstract class MyClass<T> where T : class
    public virtual List<T> GetAll(Expression<Func<T, bool>> exp, string orderByProperty)

The issue is to develop aFunc<T, TKey> to be used inOrderBy argument. How can I use to sort the list?Name property?


4/27/2019 1:35:26 PM

Popular Answer

According to the abstract class' description, adding the key are to the generic parameters is required in order to utilize it.

Simple order-passing via function example.

public abstract class MyClass<T, TKey> where T : class {
    public virtual List<T> GetAll(
        Expression<Func<T, bool>> exp, 
        Func<T, TKey> keySelector = null
    ) {
        var query = repository.Get(exp);

        if(orderBy != null) {
            return query.OrderBy(keySelector).ToList();

        return query.ToList();

The class might then be derived and have a default key type.

The following, for instance, utilizes astring nonetheless, that may have very well been anint , Guid , etc..

public abstract class MyClass<T> : MyClass<T, string> where T : class {


But ultimately, in order to utilize it, you must be aware of the arrangement by kind.

...GetAll(_ => _.SomeProperty == someValue, o => o.Name);
4/27/2019 2:34:26 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow