したがって、データのリストを返す関数を作成しようとしています。データには多くの「子」クラスがあり、そのうちの1つはCommentですが、日付のタイプがComment(IFステートメントでテスト)の場合は、次のようにします。 orderby編集日でIDではありません(IDはすべてのデータオブジェクトで使用できますが、編集日はコメントクラスでのみ使用できます)。したがって、TypeOfを実行する代わりに、TypeOfを実行すると、編集日に注文できますが、リストを返したいのですが、リストである必要があります。ただし、1時間以上グーグルしていて、運が見つかりません。それを変換する方法について。方法は次のとおりです。
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;
}
前もって感謝します!