vb.net使用linq按两个字段对结构集合进行排序

时间:2016-11-19 23:58:07

标签: vb.net linq sorting

我有一个相同结构的大集合(Me.OmniColl)。结构的两个属性是.partNumber和.rev。我想首先按部件号排序,然后按转速排序。

我尝试了以下内容:

Dim q = From c In Me.OmniColl Order By c.partNumber Select c
Dim r = From c In q Order By c.rev Select c

即使Me.OmniColl包含数千个条目,q和r都不会返回任何结果。我已经使用linq成功过滤了集合,但我的所有排序尝试都失败了。如果有人有解决方案,请在vb.net中显示您的代码。在此先感谢您的帮助。

[编辑] 结构:

<Serializable()> Structure Part  
    Public Workbook As String
    Public Worksheet As String
    Public Product As String
    Public partNumber As String
    Public itemNo As String
    Public rev As String
    Public partDescription As String
    Public unitOfMeasure As String
    Public partType As String
    Public purchasingCat As String
    Public Quantity As Double
    Public TotalPerProduct As Double
    Public hierarchy As String
End Structure    

[编辑] 我想找到一个解决方案,无需从Collection更改为其他类型,也无需从Structure更改为Public Class。我有一个数据缓存系统,我将序列化结构集合并在关闭程序之前将其保存在文本文件中。然后,我在程序打开时对此文本文件进行反序列化,并根据是否根据是否生成了对集合生成的文件所做的更改来编辑集合。很抱歉做了这么多的修改。

1 个答案:

答案 0 :(得分:0)

我认为我会创建一个Generic.List(Of Part)。在我的示例中,col是我班级顶部定义的Generic.List(Of Part),请将其替换为您的。{1}}。我看起来像这样:

 Dim col As New Generic.List(Of Part)

要获得List订购...

 Dim newCol As Generic.List(Of Part) = (From c In col Order By c.partNumber, c.rev).ToList()

这将根据需要订购您的商品。然后你询问了有关删除特定项目的信息。

您可以使用ForEach功能对列表中的每个元素执行特定操作

 newCol.ForEach(Sub(x)
                       If x.Workbook = condition Then
                           newCol.Remove(x)
                       End If
                End Sub)

注意:这已经过试用和测试

修改:

如果您想将Collection变为List ...

 Dim newColl As List(Of Part) = YOURCOLLECTION.Cast(Of Part).ToList