Solved

I got beat up by my homework.  Sorting a list with a LINQ querys

Posted on 2013-02-04
6
290 Views
Last Modified: 2013-02-04
In a nutshell, I need a violent push in the right direction.  I have to take a list of football team names, and write a program that displays the teams in alphabetical order, that start with the letter "B" and only appear once.

So far I have...
Public Class Form1
    Dim TeamNames() As String


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Loads the SBWinners file
        TeamNames = IO.File.ReadAllLines("SBWinners.txt")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Displays the Teams
        ListBox1.Items.Clear()
        Dim TeamQuery1 = From TeamNames In TeamNames
                         Where TeamNames.StartsWith("B")
                         Select TeamNames
        For Each TeamNames As String In TeamQuery1
            ListBox1.Items.Add(TeamNames)
        Next
        ListBox1.Items.Add("")

     
    End Sub
End Class 

Open in new window


Which narrows it down to four entries in the text file that start with B.  How do I sort out the re-occurring entry?

The Output I get Bear, Broncos, Broncos, Buccaneers.

Secondly, the answer is already in alphabetical order, only out of random chance since they occur in the text file that way.  If you want to play around with it, just use the four entries I get in the output as the text files, since the rest is junk data for the assignment at this point.
0
Comment
Question by:kiwishoepolish
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 

Author Comment

by:kiwishoepolish
ID: 38852996
So if I add the line,
Order By TeamNames

Open in new window

in the TeamQuery1
That would solve my alphabetical issue, just not my repeat... hmmm.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38853000
Look into the Distinct and Order By keywords.
0
 

Author Comment

by:kiwishoepolish
ID: 38853005
So if I drop the concept of multiple Querries, make my own life easier, I can just make one Querry as...
Dim TeamQuery = From TeamNames In TeamNames
                         Order By TeamNames
                         Where TeamNames.StartsWith("B")
                         Distinct
                         Select TeamNames

Open in new window

And I answer my own question.  Yup, chatting with myself makes this so much easier.

Anyone have any suggestions on a more "proper" way to do this?  Or is this considered no sloppy?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38853040
The only suggestion I would make is not to make the local range variable the same name as the collection you are iterating over.

e.g.

Change:

From TeamNames In TeamNames

...to:

From name In TeamNames

Complete:

Dim TeamQuery = From name In TeamNames
                Order By name
                Where name.StartsWith("B")
                Distinct
                Select name

Open in new window

0
 

Author Comment

by:kiwishoepolish
ID: 38853053
Thanks, I'm still trying to adjust this visual style of programing.  I figured this out years ago before a computer required a mouse.

Do I need to to identify name as a variable, or is that sort of automatically done? As in it inherits the state of being a string variable from its parent?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38853157
In the case of a LINQ query, you are defining the variable inline, and it is scoped only to the query itself. Variables within a LINQ query are implicitly typed:  Whatever type of thing is stored within the collection is the type the variable will become. If in your code TeamNames was a List(Of String), then name would be of type String.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question