[英]Oracle DataReader get sorted even with an ORDER BY clause in VB.NET
[英]Trying to get a sorted list in vb.net
我有两个从数据库填充的列表框(可用项和选定项)。 我可以通过这些列表之间的按钮进行添加和删除,但是当我从“选定”列表中删除时,该项目将转到“可用”列表的底部。 它不会按字母顺序排列。 因此,我正在尝试使用Linq选项。
这是我得到的错误:
Unable to cast object of type 'System.Linq.OrderedEnumerable2[System.Object,System.Object]' to type 'System.Collections.Generic.IEnumerable1[System.Web.UI.WebControls.ListItem]'.
码:
Imports System.Linq
'After adding the item to the list sort it
Dim myQuery As IEnumerable(Of ListItem)
myQuery = From result In Available.Items _
Select result _
Order By result.Text Ascending
'Rebuild the list
Available.Items.Clear()
For Each item In myQuery
Available.Items.Add(item)
Next
End Sub
由于Items
返回的ListItemCollection
仅实现IEnumerable
,而没有实现IEnumerable(Of ListItem)
,因此应将Cast
添加到查询中。 您还需要在清除之前使用ToList()
或其他可以强制执行查询评估的方法,因为LINQ查询是延迟计算的,否则将在评估查询之前清除列表!
myQuery = (
From result In Available.Items.Cast(Of ListItem)() _
Select result _
Order By result.Text Ascending
).ToList()
您可能还希望打开Option Strict On
以帮助在编译时查找其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.