2017-09-08 7 views
0

私は、ツリーのクラスを持っています。しかし、間違いがある可能性があるため、ポジションを変更する必要がある場合もあります(Order in OrderByを注文した後、これは以下のようになります)。 linqのような簡単な方法で、その文の位置を変更して、以下に示す方法でギャップを避けてください。私は私たちの例では0から始まる位置を変更したいソート特定のオブジェクトが

2 
5 
77 
1001 

0 
1 
2 
3 

方法:

Public Function GetSelectedSentencesOnSectionLevel(section As HtmlSection) As List(Of HtmlSentence) 
    Dim sentencesList As New List(Of HtmlSentence) 
     For Each exSection As HtmlSection In _htmlFactory.SectionsList 
     If exSection.Name = section.Name Then 
      Dim sentencesList As New List(Of HtmlSentence) 
      If Not IsNothing(exSection.SubSections) Then 
       For Each exSubsection As HtmlSubSection In exSection.SubSections 
        If Not IsNothing(exSubsection.SelectedSentences) Then 
        For Each exSentence As HtmlSentence In exSubsection.SelectedSentences 
          sentencesList.Add(exSentence) 
        Next 
        End If 
       Next 
      End If 
     End If  
    Next 

'sort sentences by Posiions ascending 
sentencesList = sentencesList.OrderBy(Function(x) x.Position).ToList()    
Return sentencesList 
End Function 

EDIT:ヘルパーのためのより多くのコード:

グローバルクラス:

Public Class HtmlFactory 
     Property SectionsList As List(Of HtmlSection) 

     Sub New() 
      SectionsList = New List(Of HtmlSection) 
     End Sub 

     Sub New(pSectionsList As List(Of HtmlSection)) 
      _SectionsList = pSectionsList 
     End Sub 

     Public Sub AddSection(section As HtmlSection) 
      SectionsList.Add(section) 
     End Sub 
.... 
+0

あなたが取り組んでいるコレクションは何ですか? –

+0

@BozhidarStoinevあなたは何を意味しているのですか? – Dino

+0

'' List(Of HtmlSentence) '...' 'ExSection'はどこから来たのでしょうか?文のサブセクションは何ですか? –

答えて

1

ここでは、純粋なLINQソリューションです。この例では

Dim index As Integer = -1 

Dim sectionName As String 

Dim allTheSections As List(Of HtmlSection) 

Dim sentenceList = allTheSections _ 
    .Where(Function(sect) _ 
       sect.SubSections IsNot Nothing _ 
       AndAlso sect.Name.Equals(sectionName, StringComparison.OrdinalIgnoreCase)) _ 
    .SelectMany(Function(sect) sect.SubSections) _ 
    .Where(Function(subSect) subSect.SelectedSentences IsNot Nothing) _ 
    .SelectMany(Function(subSect) subSect.SelectedSentences) _ 
    .OrderBy(Function(ss) ss.Position) _ 
    .Select(Function(ss) 
       index += 1 
       Return New HtmlSentence With {.Position = index, .Sentence = ss.Sentence} 
      End Function) _ 
    .ToList() 

あなたexSectionから来ない場合は、allTheSectionsです。

+0

したがって、List(Of HtmlSections)をallTheSections変数に渡し、次にsectionName変数に渡す必要があります。再生するセクションの名前を渡す必要があります。linqは0からカウントして位置を変更します(現在の順序を維持することが重要です)。それがコレクションのように休みを取る?それは本当ですか? – Dino

+0

しかし、カウントを0から始めるには、 'index'を-1に初期化する必要があります。私。 'Dim index As Integer = -1' –

+0

あなたのコードを少し変更し、ラインインデックス+ = 1の後にss.Position = indexを追加しました。これはoryginalコレクションに新しい位置を保存したいからです。しかし、あなたのコードが現在のポジションの順序に注意を払っていないという問題があります。たとえば、私たちの前に:Sentence_1のpos = 99、Sentence_2のposが6、Sentence_3のposが3だった場合、 Sentence_1 pos = 3 Sentence_2 pos 2 Sentence_3 pos 1.私のポイントを得ることを願っています。テストコードを使用することができます:https://pastebin.com/XU9UvDR0 – Dino

関連する問題