Related Topics:
Create Expression<Func<T, TKey>>
dynamically
I searched on the internet but all samples explain Expression<Func<
. How I can dynamically create a Func<T, TKey>
from T
?
Thanks
Edit 1)
The T
type in my code determine in runtime and for example I want to sort my list with Name
. Now How I can create this : Func<T, TKey> = o=>o.Name;
Edit 2)
Please consider this:
public abstract class MyClass<T> where T : class
{
public virtual List<T> GetAll(Expression<Func<T, bool>> exp, string orderByProperty)
{
repository.Get(exp).OrderBy(?);
}
}
The problem is create a Func<T, TKey>
for using in OrderBy
argument. How can I sort the list using Name
property?
Thanks
Func<>
object? Where do you need to provide this information? What do you want to do? How have you planned to do what you are trying to do? Please extend the description/problem in your question.PropertyInfo
, get the suitable getter method from that, and callDelegate.CreateDelegate
. If you could provide a minimal reproducible example of what you're trying to achieve, it would be easier to check that and provide an answer.Name
(and notFooBar
) comes from? Neither the class nor the method has indication for that. Also if it is astring
, there are several solutions on SO - stackoverflow.com/questions/39908403/…, stackoverflow.com/questions/8398453/… etc.