Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-02-04
6
Medium Priority
?
294 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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