如何使用LINQ从多维列表中删除项目

时间:2010-07-28 05:58:21

标签: vb.net linq

    Public Class GroupSelect
Public Property RowNo() As Integer
    Get
        Return m_RowNo
    End Get
    Set(ByVal value As Integer)
        m_RowNo = value
    End Set
End Property
Private m_RowNo As Integer
Public Property GroupNo() As Integer
    Get
        Return m_GroupNo
    End Get
    Set(ByVal value As Integer)
        m_GroupNo = value
    End Set
End Property
Private m_GroupNo As Integer

结束班

//Here I need to write LINQ statement and replace below code

For Each item As GroupSelect In grpSelectionList
                    If item.RowNo = rowNo And item.GroupNo = grpNo Then
                        grpSelectionList.Remove(item)
                 End If
                Next

1 个答案:

答案 0 :(得分:2)

LINQ如何帮助,特别是在VB.NET中?

无论如何,你似乎根本没有多维列表,而是一个类集合。

如果您想要一个没有这些项目的新列表,这应该有效:

grpSelectionList = grpSelectionList _
.Where(Function(g) g.RowNo <> RowNo AndAlso g.GroupNo <> grpNo).ToList()

在类似查询的语法中:

Dim g = From g in grpSelectionList _
Where g.RowNo <> RowNo AndAlso g.GroupNo <> grpNo _
Select g

grpSelectionList = g.ToList()

当你正在修改正在迭代的集合时,你当前拥有的东西不应该工作。无论如何你可以这样做:

Dim tempList as List(Of GroupSelect) = grpSelectionList

tempList _
.Where(Function(g) g.RowNo = RowNo AndAlso g.GroupNo = grpNo) _
.ToList() _
.ForEach(Function(g) grpSelectionList.Remove(g))