Visual Basic Add query to write all lines

James Carey 21 Reputation points
2021-04-07T22:22:45.957+00:00

I am combining three text files into one text file and in the one final text file, it needed to be sorted by state alphabetically. How do I get my query to work with the writealltext? My code is below:

 Private Sub generatebtn_Click(sender As Object, e As EventArgs) Handles generatebtn.Click
        Dim sen = IO.File.ReadLines("C:\temp\Senate113.txt").ToList()
        sen.AddRange(IO.File.ReadLines("C:\temp\RetiredSen.txt"))
        sen.AddRange(IO.File.ReadLines("C:\temp\NewSen.txt"))

        Dim query = From line In sen
                    Let state = Split(","c)(1)
                    Let name = Split(","c)(0)
                    Let party = Split(","c)(2)
                    Order By state Ascending
                    Select name, state, party

        IO.File.WriteAllLines("Senate114.txt", sen)

    End Sub
End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,583 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-04-08T02:53:48.44+00:00

    Hi @James Carey ,

    I am combining three text files into one text file and in the one final text file, it needed to be sorted by state alphabetically

    Is the following code helpful?

    File.WriteAllLines("Senate114.txt", sen.Distinct().OrderBy(Function(x) x.Split(","c)(1)))  
    

    If I have any misunderstanding, please let me know.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Viorel 112.7K Reputation points
    2021-04-08T04:35:23.49+00:00

    You already have an Order By, but in addition you probably want to separate the values by comma. Then try something like this:

    Dim query = From line In sen
                Let s = Split(","c)
                Let state = s(1)
                Let name = s(0)
                Let party = s(2)
                Order By state Ascending
                Select name & "," & state & "," & party
    
    0 comments No comments

  2. Karen Payne MVP 35,196 Reputation points
    2021-04-08T12:30:09.157+00:00

    Even though you have an accepted solution that is done with LINQ when dealing with files that may not be in a specific format or something unknown happens the one line solution should be replaced with one that provides exception handling along with utilizing (in this case) an interface for custom sorting, TextFieldParser for reading files into a list via a Sorted set that implements the custom IComparer interface.

    In the code sample I take three files, combine into one then export to a new file. A DataGridView provides confirmation the results are correct prior to exporting.

    Note sorting is done on product name in this code sample

    Implmentation form code

    Public Class Form1  
      
        Private Sub CombineFilesButton_Click(sender As Object, e As EventArgs) Handles CombineFilesButton.Click  
      
            DataGridView1.DataSource = Nothing  
            ExportListButton.Enabled = False  
      
            Dim products = FileOperations.Read()  
      
            If products.Count > 0 Then  
                DataGridView1.DataSource = products  
                ExportListButton.Enabled = True  
            End If  
      
        End Sub  
      
        Private Sub ExportListButton_Click(sender As Object, e As EventArgs) Handles ExportListButton.Click  
      
            If DataGridView1.DataSource IsNot Nothing Then  
                FileOperations.Export(CType(DataGridView1.DataSource, List(Of Product)))  
            End If  
      
        End Sub  
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
            AddHandler FileOperations.ExceptionHandler, AddressOf GeneralExceptionWhileReadingFile  
            AddHandler FileOperations.MalformedLineExceptionHandler, AddressOf MalFormExceptionWhileReadingFile  
      
            ExportListButton.Enabled = False  
        End Sub  
      
        Private Sub MalFormExceptionWhileReadingFile(exception As Microsoft.VisualBasic.FileIO.MalformedLineException, fileName As String)  
            Console.WriteLine($"Read error on line {exception.LineNumber} with file {fileName}")  
        End Sub  
      
        Private Sub GeneralExceptionWhileReadingFile(exception As Exception, fileName As String)  
            Console.WriteLine($"General error: {exception.Message}  with file {fileName}")  
        End Sub  
    End Class  
    

    85740-sorted.png

    85812-figure1.png

    0 comments No comments