' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Collections.Generic

Imports System.Linq
Imports System.Data
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.Data.Linq.SqlClient

Namespace DAL
    <Table(Name := "t1")> _
    Public Class T1
        <Column(Name := "f1", IsPrimaryKey := True)> _
        Public Property F1() As Integer
        <Column(Name := "f2")> _
        Public Property F2() As String
        Public Sub New()
            Me.New(0, "")
        End Sub
        Public Sub New(_f1 As Integer, _f2 As String)
            F1 = _f1
            F2 = _f2
        End Sub
        Public Overrides Function ToString() As String
            Return String.Format("[{0},{1}]", F1, F2)
        End Function
    End Class
    Public Interface IDAL
        Function GetOneByPK(f1 As Integer) As T1
        Function GetAll() As List(Of T1)
        Function GetRangeOfPK(f1_start As Integer, f1_end As Integer) As List(Of T1)
        Function GetContainingF2(f2 As String) As List(Of T1)
        Sub SaveChanges(o As T1)
        Sub SaveNew(o As T1)
        Sub Remove(f1 As Integer)
    End Interface
End Namespace

Namespace DAL.LINQtoSQL
    Public Class LINQtoSQLDAL
        Implements IDAL
        Private constr As String
        Public Sub New(constr As String)
            Me.constr = constr
        End Sub
        Public Function GetOneByPK(f1 As Integer) As T1 Implements IDAL.GetOneByPK
            Using db As New DataContext(New SqlConnection(constr))
                Return db.GetTable(Of T1)().FirstOrDefault(Function(t1) t1.F1 = f1)
            End Using
        End Function
        Public Function GetAll() As List(Of T1) Implements IDAL.GetAll
            Using db As New DataContext(New SqlConnection(constr))
                Return db.GetTable(Of T1)().ToList()
            End Using
        End Function
        Public Function GetRangeOfPK(f1_start As Integer, f1_end As Integer) As List(Of T1) Implements IDAL.GetRangeOfPK
            Using db As New DataContext(New SqlConnection(constr))
                Return db.GetTable(Of T1)().Where(Function(t1) f1_start <= t1.F1 AndAlso t1.F1 <= f1_end).ToList()
            End Using
        End Function
        Public Function GetContainingF2(f2 As String) As List(Of T1) Implements IDAL.GetContainingF2
            Using db As New DataContext(New SqlConnection(constr))
                Return db.GetTable(Of T1)().Where(Function(t1) t1.F2.Contains(f2)).ToList()
            End Using
        End Function
        Public Sub SaveChanges(o As T1) Implements IDAL.SaveChanges
            Using db As New DataContext(New SqlConnection(constr))
                Dim orig As T1 = db.GetTable(Of T1)().FirstOrDefault(Function(t1) t1.F1 = o.F1)
                orig.F2 = o.F2
                db.SubmitChanges()
            End Using
        End Sub
        Public Sub SaveNew(o As T1) Implements IDAL.SaveNew
            Using db As New DataContext(New SqlConnection(constr))
                db.GetTable(Of T1)().InsertOnSubmit(o)
                db.SubmitChanges()
            End Using
        End Sub
        Public Sub Remove(f1 As Integer) Implements IDAL.Remove
            Using db As New DataContext(New SqlConnection(constr))
                db.GetTable(Of T1)().DeleteOnSubmit(db.GetTable(Of T1)().FirstOrDefault(Function(t1) t1.F1 = f1))
                db.SubmitChanges()
            End Using
        End Sub
    End Class
End Namespace

Namespace Demo
    Public Class Program
        Public Shared Sub UpdateF2(dal As DAL.IDAL, f1 As Integer, f2 As String)
            Dim o As DAL.T1 = dal.GetOneByPK(f1)
            o.F2 = f2
            dal.SaveChanges(o)
        End Sub
        Public Shared Sub Print(lst As List(Of DAL.T1))
            Console.Write("{")
            For i As Integer = 0 To lst.Count - 1
                If i > 0 Then
                    Console.Write(",")
                End If
                Console.Write(lst(i))
            Next
            Console.WriteLine("}")
        End Sub
        Public Shared Sub Main(args As String())
            Dim dal As DAL.IDAL = New DAL.LINQtoSQL.LINQtoSQLDAL("Server=ARNEPC4\SQLEXPRESS;Integrated Security=true;Database=Test")
            Console.WriteLine(dal.GetOneByPK(2))
            UpdateF2(dal, 2, "BBB")
            Console.WriteLine(dal.GetOneByPK(2))
            UpdateF2(dal, 2, "BB")
            Console.WriteLine(dal.GetOneByPK(2))
            Print(dal.GetAll())
            Print(dal.GetRangeOfPK(2, 4))
            Print(dal.GetContainingF2("B"))
            dal.SaveNew(New DAL.T1(999, "XXX"))
            Print(dal.GetAll())
            dal.Remove(999)
            Print(dal.GetAll())
        End Sub
    End Class
End Namespace
