Solved

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

Posted on 2013-02-04
6
282 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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