Imports System.Data.Objects
Imports BDNotasAlumnos

Class MainWindow

  Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    ' Obtener el contexto de objetos
    Dim contextoDeObjs As New bd_notasAlumnosEntities()

    '' Obtener la tabla alumnos
    'Dim TablaAlumnos As ObjectQuery(Of alumno) = _
    '  contextoDeObjs.alumnos
    '' Realizar una consulta
    'Dim alums = From alum In TablaAlumnos _
    '    Where alum.id_alumno = 1234567 _
    '    Select New With {.Nombre = alum.nombre}
    'If alums.Count() > 0 Then
    '  Console.WriteLine(alums.First().Nombre)
    'End If

    'Dim alums = From alum In contextoDeObjs.alumnos _
    '    Select alum
    'Console.WriteLine(contextoDeObjs.Connection.State.ToString()) ' escribe: Closed
    'For Each alum In alums
    '  ' ...
    '  Console.WriteLine(contextoDeObjs.Connection.State.ToString()) ' escribe: Open
    'Next
    'Console.WriteLine(contextoDeObjs.Connection.State.ToString()) ' escribe: Closed

    'For Each alum In alums.ToList()
    '  ' ...
    '  Console.WriteLine(contextoDeObjs.Connection.State.ToString()) ' escribe: Closed
    'Next

    'Dim actas = _
    '    From alum In contextoDeObjs.alumnos _
    '    From al_as In alum.alums_asigs _
    '    Group New With {alum.nombre, al_as.nota} _
    '    By al_as.id_asignatura Into gr = Group

    'For Each grupo In actas
    '  Console.WriteLine(vbLf & "Asignatura: " & grupo.id_asignatura)
    '  For Each al In grupo.gr
    '    Console.WriteLine((al.nombre & " ") & al.nota)
    '  Next
    'Next

    'Dim actas = _
    '    From alum In contextoDeObjs.alumnos _
    '      Join al_as In contextoDeObjs.alums_asigs _
    '      On alum.id_alumno Equals al_as.id_alumno _
    '    Group New With {alum.nombre, al_as.nota} _
    '    By al_as.id_asignatura Into gr = Group

    'For Each grupo In actas
    '  Console.WriteLine(vbLf & "Asignatura: " & grupo.id_asignatura)
    '  For Each al In grupo.gr
    '    Console.WriteLine((al.nombre & " ") & al.nota)
    '  Next
    'Next

    'Dim idAlumno As Integer = 1234567
    'Dim alumAsigs = _
    '    From al_as In contextoDeObjs.alums_asigs _
    '    Where al_as.id_alumno = idAlumno _
    '    Group New With {.NombAsig = al_as.asignatura.nombre} _
    '    By al_as.alumno.nombre Into gr = Group

    'For Each grupo In alumAsigs
    '  Console.WriteLine(vbLf & "Alumno: " & grupo.nombre)
    '  For Each x In grupo.gr
    '    Console.WriteLine(x.NombAsig)
    '  Next
    'Next

    Dim idAlumno As Integer = 1234567
    Dim alums = _
      From al In contextoDeObjs.alumnos _
      Where al.id_alumno = idAlumno _
      Select al
    Dim asigs = _
      From al_as In contextoDeObjs.alums_asigs _
      Where al_as.id_alumno = idAlumno _
      Select al_as.asignatura

    For Each alum In alums
      Console.WriteLine(alum.nombre)
    Next
    For Each asig In asigs
      Console.WriteLine(asig.nombre)
    Next

    tbNomAlum.DataContext = alums
    tbNomAlum.Text = alums.First().nombre
    listaAsigs.ItemsSource = asigs
    listaAsigs.DisplayMemberPath = "nombre"
  End Sub
End Class
