So I am trying to make a Function which returns a list of data, data has many “child” classes and one of them is Comment however, if the type of date is Comment (Which I test with the IF statement) then I want to orderby Edit date and NOT by id (ID is available on all data objects, while edit date is only available on the comments class). So instead of doing TypeOf I do TypeOf and thenI am able to order it on the edit date however, it want to return a List then but I need it to be a List, However I have been googleing for an hour + and no luck finding on how to convert it. Here is the method:
private List<T> GetAllData<T>() where T : Data, new()
{
List<T> TmpList = new List<T>();
if (typeof(T) == typeof(Comment))
{
TmpList = _newdb.Data.OfType<Comment>().OrderBy(x => x.EditDate).ToList();
// THIS ABOVE doesn t work ofc since it wants to return a List<Comment> but TmpList is a List<T>
//List<Comment> TmpComments = _newdb.Data.OfType<Comment>().OrderBy(x => x.EditDate).ToList();
// List<T> tempzor = new List<T>(TmpComments.ToList());
//TmpList = (List<T>(TmpComments));
// TmpList = TmpComments.ConvertAll;
}
else
{
TmpList = _newdb.Data.OfType<T>().OrderBy(x => (x.Id)).ToList();
}
return TmpList;
}
Thanks in advance!
You can cast the whole list: