none
Question about Linq? RRS feed

  • Question

  • Hi, This is my first time coming to LinQ
    I have read this code and I have not understood the Order by Length statement
    Where is the length from? Why does the code understand it?
    Thanks so much, here is vb code

    Dim sentence = "the quick brown fox jumps over the lazy dog"
    ' Split the string into individual words to create a collection.
    Dim words = sentence.Split(" "c)
    
    Dim query = From word  In words
                Group word.ToUpper() By word.Length Into gr = Group
                Order By length
                Select length, GroupedWords = gr
    
    Dim output As New System.Text.StringBuilder
    For Each obj In query
        output.AppendLine(String.Format("Words of length {0}:", obj.Length))
        For Each word As String In obj.GroupedWords
            output.AppendLine(word)
        Next
    Next

    Sunday, December 8, 2019 12:33 PM

Answers

  • No. You did not understand my question. Exactly The group statement encapsulates the word and word.length, which also terminates the range variable = word in this query.
    and subsequent comands can only be called by 'gr' or property have been called

    • Edited by AU Gridlock Sunday, December 8, 2019 7:52 PM
    • Marked as answer by AU Gridlock Monday, December 9, 2019 12:27 PM
    Sunday, December 8, 2019 7:52 PM

All replies

  • Since word is a string it has a property of Length and GroupWords is IEnumerable(Of String).


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, December 8, 2019 1:03 PM
    Moderator
  • Hi Kareninstructor,


    I mean why is not Order by word.length I often see the Order by Column1 statement, I think it is a variable or a certain name is not a property.
    • Edited by AU Gridlock Sunday, December 8, 2019 5:57 PM
    Sunday, December 8, 2019 5:57 PM
  • Hi Kareninstructor,


    I mean why is not Order by word.length I often see the Order by Column1 statement, I think it is a variable or a certain name is not a property.

    Whomever (and I've seen a similar or same example by Microsoft) wrote the code is demonstrating ordering by exactly what it says, length.

    You can see a better view by modifying the code to this.

    Add this class to your project

    Public Class Example
        Public Property Length() As Integer
        Public Property GroupedWords() As IEnumerable(Of String)
    End Class

    Then say in a button click event

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sentence = "the quick brown fox jumps over the lazy dog"
        ' Split the string into individual words to create a collection.
        Dim words = sentence.Split(" "c)
    
        Dim query = From word In words
                    Group word.ToUpper() By word.Length Into gr = Group
                    Order By Length
                    Select New Example With {.Length = Length, .GroupedWords = gr}
    
        Dim output As New System.Text.StringBuilder
        For Each item As Example In query
            Console.WriteLine(item.Length)
            output.AppendLine($"Words of length {item.Length}:")
            For Each word As String In item.GroupedWords
                Console.WriteLine($"  {word}")
            Next
        Next
    End Sub

    Run with the IDE output window open and get the following.

    3
      THE
      FOX
      THE
      DOG
    4
      OVER
      LAZY
    5
      QUICK
      BROWN
      JUMPS
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, December 8, 2019 6:31 PM
    Moderator
  • Hi, Because this code will not work
    Dim query = From word In words  
               Order By Length             
               Select word, ....
    so I don't know the role of length, does it only work after the group statement?
    Sunday, December 8, 2019 6:40 PM
  • What did the group statement actually do, it created a class and contained the Length property? What other Propertys are there? how do i know them. affordable based on intellisense from word.
    Yes. I'm having trouble understanding this code
    • Edited by AU Gridlock Sunday, December 8, 2019 6:53 PM
    Sunday, December 8, 2019 6:51 PM
  • What did the group statement actually do, it created a class and contained the Length property? What other Propertys are there? how do i know them. affordable based on intellisense from word.
    Yes. I'm having trouble understanding this code

    You first really need to read the documentation on Group By to understand the clause. I already explain Length in my first reply.

    What other properties? of what?

    In regards to knowing properties of any object is not by Intellisense which is okay but truly knowing properties is first knowing a type to check it's properties.

    To know properties and methods read about them e.g. start here.

    The worst thing to do is be blind to reading Microsoft documentations and simply picking code samples off the web.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, December 8, 2019 7:08 PM
    Moderator
  • No. You did not understand my question. Exactly The group statement encapsulates the word and word.length, which also terminates the range variable = word in this query.
    and subsequent comands can only be called by 'gr' or property have been called

    • Edited by AU Gridlock Sunday, December 8, 2019 7:52 PM
    • Marked as answer by AU Gridlock Monday, December 9, 2019 12:27 PM
    Sunday, December 8, 2019 7:52 PM
  • I figured it out. very nice. thanks you 
    Sunday, December 8, 2019 7:53 PM
  • Hi,

    Did you resolve the issue?

    If you have resolved the issue then please mark the post or posts that helped you as The Answer using the Mark as Answer link at the bottom of the post. Marking answers will help other community members find solutions in the future.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 9, 2019 2:15 AM
    Moderator