This is a migrated thread and some comments may be shown as answers.
Linq Groupping two columns
0 Answers 8 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Top achievements
Rank 1
Rena asked on 19 Nov 2012, 08:55 PM
Hello everyone,

I write the first time in this forum! I have write the code bellow and it worked well for two years. I must write that I really like linq but I don't have the opportunity to work very often with linq because I primary am a DB developer. So, the code bellow reads a text file, splits it in columns and then the Linq query selects and groups the second column value. The propose for this code is to split the files based the value of the second column. I want to add a second column to that grouping, the column 1. The target is to have this result -->
column1_second _value_column2_second_value.txt ...etc
Can anyone help me?

Thank you!!!

 <?xml version="1.0" encoding="utf-8" ?>
    <add key="FileDelimiter" value=";" />
    <add key="FilePreffix" value="invoice_" />
    <add key="IndexGroup" value="2" />

Sub splittFiles()
        Dim FileIn As String
        Dim bPath As String
        bPath = AppSettings.Get("BackupPath") & DateFormat(Today, "_")
        If Dir(bPath) = "" Then Directory.CreateDirectory(bPath)
        FileIn = Dir(AppSettings.Get("InputPath") & "*.txt", vbDirectory)
            Do While FileIn <> ""
                Dim fileA As String() = System.IO.File.ReadAllLines(AppSettings.Get("InputPath") & FileIn)
                ' Concatenate and remove duplicate names based on
                Dim mergeQuery As IEnumerable(Of String) = fileA

                ' Group the names by the Invoice number
                Dim groupQuery = From name In mergeQuery _
                             Let n = name.Split(New Char() {(AppSettings.Get("FileDelimiter").ToString)}) _
                             Group By groupKey = n(CType(AppSettings.Get("IndexGroup").ToString, Integer)) _
                             Into groupName = Group

                ' Create a new file for each group that was created
                ' Note that nested foreach loops are required to access
                ' individual items with each group.

                For Each gGroup In groupQuery
                    Dim fileName As String = AppSettings.Get("OutputPath") & AppSettings.Get("FilePreffix") & Trim(gGroup.groupKey) & ".txt"
                    ''If the File Already exists add Date to the end of the file
                    If File.Exists(fileName) Then
                        fileName = AppSettings.Get("OutputPath") & AppSettings.Get("FilePreffix") & Trim(gGroup.groupKey) _
                        & Year(Now) & Month(Now) & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & ".txt"
                    End If
                    Dim sw As New System.IO.StreamWriter(fileName)
                    For Each item In gGroup.groupName
                        Console.WriteLine("   " &

     File.Move(AppSettings.Get("InputPath") & FileIn, bPath & "\" & FileIn.Replace(".txt", "_") _
                  & Year(Now) & Month(Now) & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & ".txt")

                FileIn = Dir()
        Catch ex As Exception
        End Try
    End Sub
LINQ (LINQ specific questions)
Asked by
Top achievements
Rank 1
Share this question