question

JamesCarey-0553 avatar image
0 Votes"
JamesCarey-0553 asked karenpayneoregon edited

Visual Basic Add query to write all lines


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




dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
1 Vote"
XingyuZhao-MSFT answered XingyuZhao-MSFT commented

Hi @JamesCarey-0553 ,

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is exactly it! How do I prevent duplicates from in the text file though?

0 Votes 0 ·

Hi @JamesCarey-0553 ,
Check: Enumerable.Distinct method, I have updated my code.


0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered JamesCarey-0553 commented

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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





sorted.png (20.9 KiB)
figure1.png (8.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.