Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-04
6
Medium Priority
?
296 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

971 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