2016-03-24 13 views
0

Ich mag/Check x ändern (5) mit einem if-Anweisung
wenn x (5) = 270 dann x (5) = -90
Gibt es eine Möglichkeit zu Greife auf das Array zu.vb.net Zugang Felder nach line.split

Sub Main() 
    Dim filePath, filePathOut As String 
    Dim fileName As String 
    fileName = "test.txt" 
    filePath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, fileName) 
    filePathOut = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "out\" & fileName) 
    ' Create the IEnumerable data source. 
    Dim lines As String() = System.IO.File.ReadAllLines(filePath) 


    Dim lineQuery = From line In lines 
        Let x = line.Split(New Char() {","}) 
        Order By x(0) 
        Select x(0) & ", " & x(3) & ", " & x(5) 

    System.IO.File.WriteAllLines(filePathOut, lineQuery.ToArray()) 

    Console.WriteLine("Spreadsheet2.csv written to disk. Press any key to exit") 
    Console.ReadKey() 
End Sub 
+0

was ist 'c' in' c (5) = -90' ?? – har07

+0

Typ Fehler x (5) nicht c (5) I Fehler behoben jetzt –

Antwort

0

Meinten Sie, Wert für die 3. CSV-Spalte bedingt zurückzugeben, etwas wie das?

Dim lineQuery = From line In lines 
       Let x = line.Split(New Char() {","}) 
       Order By x(0) 
       Select x(0) & ", " & x(3) & ", " & If(x(5) = "270", "-90", x(5)) 

Für Referenz: Is there a conditional ternary operator in VB.NET?

+0

getestet ... und es funktioniert! –

+0

@DanielGroulx vergiss nicht [akzeptiere eine Antwort] (http://stackoverflow.com/help/someone-answers), die du siehst, löst das Problem in Frage – har07

0

Vielleicht wird so etwas funktionieren.

Dim lineQuery = From line In lines 
        Let x = line.Split(New Char() {","}) 
        Order By x(0) 
        Where x(5) = 270 
        Select x(5) = -90 And x(0) & ", " & x(3) & ", " & x(5) 

Eine ausführlichere Antwort kann here finden.

+0

Dies wählt nur Datensätze mit x (5) = "270". Ich brauchte alle Datensätze, aber ändern x (5) zu -90, wenn es 270 war –

+0

Versuchen Sie Folgendes: Dim lineQuery = Von Zeile In Zeilen Lassen Sie x = line.Split (New Char() {","}) Order By x (0) Wobei x (5) = 270 Wählen Sie x (5) = -90 Und x (0) & "," & x (3) & "," & x (5) –

1

Eine Sache, mit der aktuellen Methode zu prüfen, Linq verwenden ist, wenn es Linien, die auf Ihre erwarteten Daten in der Textdatei nicht entsprechen. Damit erlaubt das Folgende Probleme, z.B. Zeile hat nicht die erwartete Anzahl von Spalten oder die Zeile ist leer.

Es gibt mehr Code als eine Ein-Zeilen-Lambda oder Linq-Anweisung, aber wie oben angegeben, um die Verwendung von Assertion zu ermöglichen.

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