Одна вещь, чтобы рассмотреть с текущим методом с использованием LINQ, если есть строки, которые не соответствуют вашим ожидаемым данным в текстовом файле. При этом следующие проблемы допускают, например, проблемы. строка не имеет ожидаемого количества столбцов или строка пуста.
Существует больше кода, чем однострочный оператор lambda или linq, но как указано выше, чтобы разрешить использование утверждения.
Imports Microsoft.VisualBasic.FileIO
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim readFile As String =
IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Textfile1.txt")
Dim writeFile As String =
IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Textfile2.txt")
Dim Lines As New List(Of Line)
Using reader As New TextFieldParser(readFile)
reader.TextFieldType = FieldType.Delimited
reader.Delimiters = New String() {","c}
Dim currentRow As String()
While Not reader.EndOfData
'
' Feel free to discard the try/catch if so be it
'
Try
currentRow = reader.ReadFields()
Lines.Add(New Line(currentRow))
Catch ex As MalformedLineException
' decide how to handle
End Try
End While
End Using
IO.File.WriteAllLines(writeFile,
Lines.OrderBy(Function(item) item.Column0) _
.Select(Function(item) item.ItemArray).ToArray)
End Sub
End Class
''' <summary>
''' Used to create a line item that will
''' be inserted into a List(Of Line) in the caller
''' reading a text file.
''' </summary>
''' <remarks>
''' Gave generic names to properties, we could do an
''' an anonymous type inside of Lambda or Linq and done
''' away with the class but felt this is simply another
''' option
''' </remarks>
Public Class Line
Public Property Column0 As String
Public Property Column1 As String
Public Property Column2 As String
Public Property Column3 As String
Public Property Column4 As String
Public Property Column5 As String
Public Sub New(ByVal sender As String())
If sender.Length = 6 Then
Column0 = sender(0)
Column1 = sender(1)
Column2 = sender(2)
Column3 = sender(3)
Column4 = sender(4)
If sender(5) = "270" Then
Column5 = "-90"
Else
Column5 = "270"
End If
Else
' decide how to handle incorrect columns in a line
End If
End Sub
Public Function ItemArray() As String
Return String.Format("{0},{1},{2},{3},{4},{5}",
Column0, Column1, Column2, Column3, Column4, Column5)
End Function
End Class
Что такое 'c' в' c (5) = -90' ?? – har07
type error x (5) not c (5) I исправлена ошибка –