Results 1 to 5 of 5

Thread: LINQ to get an Item from an array and Debug.WriteLine it.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    LINQ to get an Item from an array and Debug.WriteLine it.

    Starting to use LINQ. Using this LINQ code to Filter in strings in an array that Contain the word Visual.

    Code:
    Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
    
            Dim selectedWords = From word In words
                                Where word.ToUpper.Contains("VISUAL")
                                Select word
    
            For Each word In words
                Debug.WriteLine(word.ToString)
            Next
    The result is :

    Visual Basic
    Java
    Visual Studio

    Not the one I expected and looking for. With Array.Filter works ok but I what to learn how to do it with LINQ. The output should be:

    Visual Basic
    Visual Studio

    What is wrong ?. Thank you.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: LINQ to get an Item from an array and Debug.WriteLine it.

    Solved it.

    Code:
     Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
    
            Dim selectedWords = From word In words
                                Where word.ToUpper.Contains("VISUAL")
                                Select word
    
            For Each wrd In selectedWords
                Debug.WriteLine(wrd.ToString)
            Next
    RESULT:

    Visual Basic
    Visual Studio

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: LINQ to get an Item from an array and Debug.WriteLine it.

    For the record here is another method

    Code:
    Module Module1
    Sub Main()
        Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
        Dim selectedWords = From item In
                            words Where item.IndexOf("visual", StringComparison.CurrentCultureIgnoreCase) > -1
        For Each word As String In selectedWords
            Console.WriteLine($"->{word}")
        Next
        Console.ReadLine()
    End Sub
    End Module

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: LINQ to get an Item from an array and Debug.WriteLine it.

    well heck, as long as we're showing off here....
    Code:
            Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
            Array.ForEach(words.Where(Function(w) w.Contains("Visual")).ToArray, Sub(w) Console.WriteLine(String.Format("-> {0}", w)))
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: LINQ to get an Item from an array and Debug.WriteLine it.

    Mark this thread as resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width