Ist bei der Suche ein Linq-Approach möglich?

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von Bartosz.

    Ist bei der Suche ein Linq-Approach möglich?

    Moin Leute.
    Ich wollte inmal fragen, ob ihr hier kurz bitte rüberschauen könntet und ob man bei der Suche (
    TextBox_Search_KeyDown) etwas verbessern kann, am liebsten mit Linq.

    Edit
    Ich frage mich, ob die 3 For-Each-Schleifen wegkönnen.
    Die Klassenstruktur soll erhalten bleiebn (ist in Wirklichkeit noch etwas mehr im letzten innersten If, was abgefragt wird).
    Die Bitmaps müssen neu instanziiert werden, sonst wird auf die alten gezeigt.


    Klasse Post
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public NotInheritable Class Post
    2. Public Property Paths As List(Of String)
    3. Public Property Images As List(Of Drawing.Bitmap)
    4. Public Property AreImagesLoaded As Boolean()
    5. ''' <summary>
    6. ''' Überschrift
    7. ''' </summary>
    8. ''' <returns></returns>
    9. Public Property Caption As String
    10. Public Property CreationDate As Date
    11. Public Sub New(Caption As String, CreationDate As Date, Paths As List(Of String), Images As List(Of Drawing.Bitmap))
    12. Me.Caption = Caption
    13. Me.CreationDate = CreationDate
    14. Me.Paths = Paths
    15. Me.Images = Images
    16. End Sub
    17. End Class


    Klasse Thread
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public NotInheritable Class Thread
    2. ''' <summary>
    3. ''' Überschrift
    4. ''' </summary>
    5. ''' <returns></returns>
    6. Public Property Caption As String
    7. Public Property CreationDate As Date
    8. Public Property ListOfPosts As List(Of Post)
    9. Public Sub New(Caption As String, CreationDate As Date, ListOfPosts As List(Of Post))
    10. Me.Caption = Caption
    11. Me.CreationDate = CreationDate
    12. Me.ListOfPosts = ListOfPosts
    13. End Sub
    14. End Class


    FormMain (auf's Wesentliche beschränkt)
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public NotInheritable Class FormMain
    2. Private ReadOnly allThreads As New List(Of Thread)
    3. Private Sub Button_new_Thread_Click(sender As Object, e As EventArgs) Handles Button_new_Thread.Click
    4. allThreads.Clear()
    5. Dim Path1 As New List(Of String) From {"C:\Users\Name\Pictures\BlackBackground.png"}
    6. Dim Image1 As New List(Of Drawing.Bitmap)
    7. If System.IO.File.Exists("C:\Users\Name\Pictures\BlackBackground.png") Then
    8. Image1.Add(New Drawing.Bitmap("C:\Users\Name\Pictures\BlackBackground.png"))
    9. End If
    10. Dim Path2 As New List(Of String) From {"C:\Users\Name\Pictures\RedBackground.png"}
    11. Dim Image2 As New List(Of Drawing.Bitmap)
    12. If System.IO.File.Exists("C:\Users\Name\Pictures\RedBackground.png") Then
    13. Image2.Add(New Drawing.Bitmap("C:\Users\Name\Pictures\RedBackground.png"))
    14. End If
    15. Dim Path3 As New List(Of String) From {"C:\Users\Name\Pictures\LightBlueBackground.png"}
    16. Dim Image3 As New List(Of Drawing.Bitmap)
    17. If System.IO.File.Exists("C:\Users\Name\Pictures\LightBlueBackground.png") Then
    18. Image3.Add(New Drawing.Bitmap("C:\Users\Name\Pictures\LightBlueBackground.png"))
    19. End If
    20. Dim Post1 As New List(Of Post) From {
    21. New Post("Postüberschrift 1", Date.Now, Path1, Image1)
    22. }
    23. allThreads.Add(New Thread("Thread-Überschrift 2", Date.Now, Post1))
    24. Dim Post2 As New List(Of Post) From {
    25. New Post("Postüberschrift 2", Date.Now, Path2, Image2)
    26. }
    27. allThreads.Add(New Thread("Thread-Überschrift 2", Date.Now, Post2))
    28. Dim Post3 As New List(Of Post) From {
    29. New Post("Postüberschrift 3", Date.Now, Path3, Image3)
    30. }
    31. allThreads.Add(New Thread("Thread-Überschrift 3", Date.Now, Post3))
    32. End Sub
    33. Private Sub TextBox_Search_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox_Search.KeyDown
    34. If Not (e.KeyCode = Keys.Enter) Then Return
    35. If System.String.IsNullOrEmpty(TextBox_Search.Text) Then Return
    36. Dim foundPosts As New List(Of Post)
    37. For Each [Thread] As Thread In allThreads
    38. For Each [Post] As Post In Thread.ListOfPosts
    39. If Post.Caption.Contains(TextBox_Search.Text) Then
    40. Debug.WriteLine($"Found »{TextBox_Search.Text}«")
    41. Dim Images As New List(Of Drawing.Bitmap)
    42. For Each Path As String In Post.Paths
    43. If IO.File.Exists(Path) Then
    44. Images.Add(New Drawing.Bitmap(Path))
    45. End If
    46. Next
    47. foundPosts.Add(New Post(Post.Caption, Post.CreationDate, Post.Paths, Images))
    48. End If
    49. Next
    50. Next
    51. End Sub
    52. Private Sub TextBox_Search_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox_Search.KeyPress
    53. If e.KeyChar = Microsoft.VisualBasic.ControlChars.Cr Then
    54. e.Handled = True
    55. End If
    56. End Sub
    57. End Class

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Bartosz“ ()

    Ein If bei For kannst Du meistens mit Where entsorgen und eine pure For-Schleife mit ForEach:

    VB.NET-Quellcode

    1. For Each [Post] As Post In Thread.ListOfPosts.Where(Function(x) x.Caption.Contains(TextBox_Search.Text))
    2. Debug.WriteLine($"Found »{TextBox_Search.Text}«")
    3. Dim Images As New List(Of Drawing.Bitmap)
    4. Post.Paths.Where(Function(x) IO.File.Exists(x)).ToList.ForEach(Sub(x) Images.Add(New Drawing.Bitmap(x))) '*
    5. foundPosts.Add(New Post(Post.Caption, Post.CreationDate, Post.Paths, Images))
    6. Next

    * ToList nur dann, wenn Du Dir selbst noch keine ForEach-Extension für IEnumerable(Of T) geschrieben hast (die m.E. im Framework fehlt)
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.