How to pass Type dynamically to TKey in a method in c#?

ykl
ykl
340 Points
17 Posts

I have following generic method sygnature:

public Result<MyAction> Search<TKey>(PageRequest request, Expression<Func<MyAction, bool>> whereExpression = null, Expression<Func<MyAction, TKey>> orderBy = null, bool isAsc = true)

Here I'm facing challenge to pass TKey during sorting as:

//sorting with datetime type
var myAction = _applicationUnitOfWork.MyAction.Search<DateTime>(pageRequest, filter, s => s.FromDate, isAsc);

//sorting with int type
var myAction = _applicationUnitOfWork.MyAction.Search<int>(pageRequest, filter, s => s.id, isAsc);

//sorting with string type
var myAction = _applicationUnitOfWork.MyAction.Search<string>(pageRequest, filter, s => s.name, isAsc);

Here in above, I'm passing TKey on the basis of type of sorting field. Is there any way to pass it dynamically?

Views: 299
Total Answered: 0
Total Marked As Answer: 0
Posted On: 30-Oct-2023 01:49

Share:   fb twitter linkedin
Answers
 Log In to Chat