' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
﻿Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text

Namespace E
    Public Class Data
        Public Property Id As Integer
        Public Property Text As String
    End Class
    Public Class CSVReader
        Private stream As StreamReader
        Private fieldsep As Char
        Private strquote As Char
        Private esc As Char
        Public Sub New(stream As StreamReader, fieldsep As Char, strquote As Char, Optional esc As Char = ControlChars.NullChar)
            Me.stream = stream
            Me.fieldsep = fieldsep
            Me.strquote = strquote
            Me.esc = esc
        End Sub
        Public Function ReadLine() As List(Of String)
            Dim line As String = stream.ReadLine()
            If line Is Nothing Then
                Return Nothing
            End If
            Dim res As New List(Of String)()
            Dim inq As Boolean = False
            Dim skip As Boolean = False
            Dim tmp As New StringBuilder("")
            For i As Integer = 0 To line.Length - 1
                Dim c As Char = line(i)
                If skip Then
                    tmp.Append(c)
                    skip = False
                ElseIf (c = fieldsep) AndAlso Not inq Then
                    res.Add(tmp.ToString())
                    tmp = New StringBuilder("")
                ElseIf (c = esc) AndAlso (i < line.Length - 1) AndAlso (line(i + 1) = strquote) Then
                    skip = True
                ElseIf (c = strquote) AndAlso ((tmp.Length = 0) OrElse inq) Then
                    inq = Not inq
                Else
                    tmp.Append(c)
                End If
            Next
            res.Add(tmp.ToString())
            Return res
        End Function
        Public Function ReadLine(Of T As {Class, New})(ParamArray asgn As Action(Of String, T)()) As T
            Dim raw As List(Of String) = ReadLine()
            If raw Is Nothing Then
                Return Nothing
            End If
            Dim o As New T()
            For i As Integer = 0 To asgn.Length - 1
                asgn(i)(raw(i), o)
            Next
            Return o
        End Function
        Public Function ReadAllLines(Of T As {Class, New})(ParamArray asgn As Action(Of String, T)()) As List(Of T)
            Dim res As New List(Of T)()
            Dim o As T = ReadLine(asgn)
            While o IsNot Nothing
                res.Add(o)
                o = ReadLine(asgn)
            End While
            Return res
        End Function
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    End Class
    Public Class Program
        Public Shared Sub Main(args As String())
            Using sr As New StreamReader("C:\work\data.csv")
                Dim csv As New CSVReader(sr, ","C, """"C, """"C)
                Dim data As List(Of Data) = csv.ReadAllLines(Of Data)(
Sub(s, o)
    o.Id = Integer.Parse(s)
End Sub,
Sub(s, o) 
    o.Text = s
End Sub
                )
                ' find the text for id 2
                Console.WriteLine(data.Find(Function(r) r.Id = 2).Text)
                ' find all id's where text contains a 'm' or 'M'
                For Each o As Data In data.Where(Function(r) r.Text.ToLower().Contains("m"C))
                    Console.WriteLine(o.Id)
                Next
                ' find the text for id 5
                Console.WriteLine(data.Find(Function(r) r.Id = 5).Text)
            End Using
        End Sub
    End Class
End Namespace
