Solved

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

Posted on 2013-02-04
6
262 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
  • 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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now