Common methods in C #
General methods are generally new to me. We need a method that returns a collection of the generic type, but also takes a collection of the same generic type and takes
Expression<Func<GenericType, DateTime?>>[] Dates
parameter. The T for the next function should be of the same type, so right now I used (simplified version):
private static Collection<T> SortCollection<T>(Collection<T> SortList, Expression<Func<T, DateTime>>[] OrderByDateTime)
{
return SortList.OrderBy(OrderByDateTime[0]);
}
but i am getting error:
Error: The type arguments for the method 'System.Linq.Enumerable.OrderBy (System.Collections.Generic.IEnumberable, System.Func)' cannot be retired. Attempt to explicitly set type arguments.
Is there a way to do this?
source to share
In this situation, the compiler does not figure out what type arguments you intend to provide to the OrderBy method, so you will need to explicitly specify them:
SortList.OrderBy<T, DateTime>(OrderByDateTime[0])
You probably want to call ToList()
if you want to return a collection
source to share
Sorry for answering twice, but this is a legitimately different solution.
You pass Expression<Func<T, DateTime>>
, but Orderby wants aFunc<T, DateTime>
You can compile the expression:
return new Collection<T>(SortList.OrderBy(OrderByDateTime[0].Compile()).ToList());
or pass direct arguments as arguments:
private static Collection<T> SortCollection<T>(Collection<T> SortList, Func<T, DateTime>[] OrderByDateTime)
{
return new Collection<T>(SortList.OrderBy(OrderByDateTime[0]).ToList());
}
I would recommend reading Expressions in msdn
source to share