1

プロジェクトのジェネリックリストを正しい順序で返す必要があり、InvalidCastExceptionエラーが発生します。コードは次のとおりです。

Dim lDt As List(Of Comment) = RemapCommentsForE1(so.CommentList). _
OrderBy(Function(x) x.CreateDate.Value). _
ThenBy(Function(x) x.Sequence). _
ThenBy(Function(x) x.SubSequence)

ご了承ください:

  • CreateDateはNullable(Of DateTimeOffset)
  • シーケンスはNullable(Of Int32)
  • サブシーケンスはNullable(Of Int32)

私が得ている正確なエラーは次のとおりです。

2[DTDataUploader.Comment,System.Int32]' to type 'System.Collections.Generic.Listタイプ'System.Linq.OrderedEnumerable1 [DTDataUploader.Comment]'のオブジェクトをキャストできません。

実際のタイプに変換してみました...

Dim lDt As List(Of Comment) = RemapCommentsForE1(so.CommentList). _
OrderBy(Function(x) x.CreateDate.Value). _
ThenBy(Function(x) Convert.ToInt32(x.Sequence)). _
ThenBy(Function(x) Convert.ToInt32(x.SubSequence))

...しかし、同じエラーが発生します。ここで何が欠けていますか?

4

2 に答える 2

2

LINQ 操作は、結果ではなくクエリWhereOrderBy生成します。エラーが示すように、完全な LINQ 式の結果は であり、リストではありません。OrderedEnumerable(Of DTDataUploader.Comment, System.Int32)

これをリストにするにToList()は、式の最後にへの呼び出しを追加します。

Dim lDt As List(Of Comment) = RemapCommentsForE1(so.CommentList). _
OrderBy(Function(x) x.CreateDate.Value). _
ThenBy(Function(x) x.Sequence). _
ThenBy(Function(x) x.SubSequence).ToList()
于 2012-06-13T18:51:00.760 に答える
0

クエリの結果は です。最後にOrderedEnumerable追加するだけで、結果をリストとして実現できます。.ToList()

于 2012-06-13T18:50:46.103 に答える