Using link to Count and Organize

Hello experts on VB.
Gosh I love this stuff.
I have a project that was never started that I get to take care of and of course they want it in VB.
What this program is a windows based program that takes a .txt file called"EmployeeVotes.txt", which is inside the project and counts the number of times an employee has been nominated for employee of the month and then displays them in order from greatest to least showing the number of votes and then the employees name in a list box. I have included my code. IT WONT WORK FOR CRUD!!! LOL  help please :)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim votes = From line In IO.File.ReadAllLines("EmployeeVotes.txt")

                  Group line By line Into Count() _

                  Order By Count Descending
   
        For Each vote In votes

            ListBox1.Items.Add(team.line & " (" & team.Count & ")")

        Next
    End Sub

Open in new window

HawaiiDragonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
Do you have a sample from the EmployeeVotes.txt file? How is it arranged?
HawaiiDragonAuthor Commented:
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
That's pretty close...you needed a continuation line after the "Dim votes" line and you were using "team" instead of "vote".  I also made it ignore blank lines in the file:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using ofd As New OpenFileDialog
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

                Dim votes = From line In IO.File.ReadAllLines(ofd.FileName) _
                    Where line.Trim.Length > 0 _
                    Group line By line Into Count() _
                    Order By Count Descending

                ListBox1.Items.Clear()
                For Each vote In votes
                    ListBox1.Items.Add(vote.line & " (" & vote.Count & ")")
                Next

            End If
        End Using
    End Sub

Open in new window

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Fernando SotoRetiredCommented:
Hi HawaiiDragon;

The code snippet below should give you what you need.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim votes = From line in System.IO.File.ReadAllLines("C:\Temp\Downloads\EmployeeVotes.txt") _
                group line by Key = line into employee = Group _
                order by employee.Count() Descending _
                where Not (Key.Trim() = String.Empty) _
                select Key & " (" & employee.Count() & ")"

    ListBox1.DataDource = votes.ToList()

End Sub

Open in new window


Fernando

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi HawaiiDragon;

In my code snippet on my last post I placed a where clause to remove blank lines such as someone adding a CrLf after the last name. I also modified the select clause so that it does the formatting of the string. And finally I added the returned value from the query using ListBox1.DataDource so that you do not need to iterate through the list.

Fernando
HawaiiDragonAuthor Commented:
Awesome I am glad that I was close but when I do this it pulls up a flie brouse window and I need it to access the EmployeVotes.txt thats in the project folder. I have tried modifying the dim votes but to no avial. Ideas? Thank you in advance.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using ofd As New OpenFileDialog
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

                Dim votes = From line In IO.File.ReadAllLines("SBWinners.txt") _
                    Where line.Trim.Length > 0 _
                    Group line By line Into Count() _
                    Order By Count Descending

                ListBox1.Items.Clear()
                For Each vote In votes
                    ListBox1.Items.Add(vote.line & " (" & team.Count & ")")
                Next

            End If
        End Using
    End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
...just remove the OpenFileDialog lines...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim votes = From line In IO.File.ReadAllLines("SBWinners.txt") _
            Where line.Trim.Length > 0 _
            Group line By line Into Count() _
            Order By Count Descending

        ListBox1.Items.Clear()
        For Each vote In votes
            ListBox1.Items.Add(vote.line & " (" & vote.Count & ")")
        Next
    End Sub

Open in new window

HawaiiDragonAuthor Commented:
scratch that i got it figured out man its been a day here. All I had to do (dumb mistake) was to comment out the openfiledialog lord!!!!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '  Using ofd As New OpenFileDialog
        'If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

        Dim teams = From line In IO.File.ReadAllLines("EmployeeVotes.txt") _
            Where line.Trim.Length > 0 _
            Group line By line Into Count() _
            Order By Count Descending

        ListBox1.Items.Clear()
        For Each vote In votes
            ListBox1.Items.Add(vote.line & " (" & team.Count & ")")
        Next

        ' End If
        ' End Using
    End Sub

End Class

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You still have "team" in there though....change it up to "vote".

This:

    ListBox1.Items.Add(vote.line & " (" & team.Count & ")")

Becomes:

    ListBox1.Items.Add(vote.line & " (" & vote.Count & ")")
HawaiiDragonAuthor Commented:
Thank you soo much all please see my revised code, Idle you posted as I was typing I never saw the email go off. YOU GUYS ARE GREAT!!!

HD
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.