.net 引发InvalidCastException的泛型列表中的OrderBy nullable属性

.net 引发InvalidCastException的泛型列表中的OrderBy nullable属性,.net,vb.net,lambda,nullable,.net,Vb.net,Lambda,Nullable,我需要为我的项目以正确的顺序返回一个通用列表,我得到了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) 请注意: CreateDa

我需要为我的项目以正确的顺序返回一个通用列表,我得到了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是一个
    可为空的(日期时间偏移量)
  • 序列是一个
    可为空的(Int32)
  • 子序列是一个
    可为空的(Int32)
我得到的确切错误是:

无法强制转换类型为的对象 'System.Linq.OrderedEnumerable
2[DTDataUploader.Comment,System.Int32]'
键入“System.Collections.Generic.List
1[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))

。。。但我也犯了同样的错误。这里我缺少了什么?

查询的结果是一个
OrderedEnumerable
,只需在末尾添加
.ToList()
,即可将结果作为列表实现。

LINQ操作,如
Where
OrderBy
生成查询,而不是结果。如错误所述,完整LINQ表达式的结果是一个
OrderedEnumerable(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()

非常感谢。我在过去做过很多次,所以我不知道为什么我没听清楚。。。但是你的超快速反应意味着我不会错过我的最后期限,它离我只有四个小时了。