In my "Invoice" class it has 20+ fields, I need to support sorting to all fields.
I have a method accepting a string of the property (e.g. "AmountDue"). Now I need to OrderBy AmoundDue field.
My specific requirement is, passing a property name string value I want to return a function like,
Func<Invoice, decimal> keySelector = a=> a.AmountDue
;
Where whole expression is dynamically built.
where I can use the keySelector
for for sorting
_api.Invoices.Find().Where(c => c.Contact.Id == contactId).OrderBy(keySelector).ToList();
So far I was able to identify the type with the string value. Need some help to return a function.
public static Type VariableType(string prop)
{
Type type = typeof(TResource);
PropertyInfo pi = type.GetProperty(prop, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
type = pi.PropertyType;
return type;
}
I referred to this post before I post this question. Inputs being considered are the same both questions, but outputs are different. I tried to use that solution and played around, but could not solve my specific problem. In my case I am calling a third party API (Xero api) and I want to build this output specifically.