Lo primero mi clase
Public Class User
Private _id As String
Private _nombre As String
Private _class As String
Public Property Clase() As String
Get
Return _class
End Get
Set(ByVal value As String)
_class = value
End Set
End Property
Public Property ID() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
Public Property Nombre() As String
Get
Return _nombre
End Get
Set(ByVal value As String)
_nombre = value
End Set
End Property
End Class
Dim lst As List(Of User) = New List(Of User)
lst.Add(New User() With {.ID = 1, .Nombre = "Ana1", .Clase = "A"})
lst.Add(New User() With {.ID = 2, .Nombre = "Ana2", .Clase = "B"})
lst.Add(New User() With {.ID = 3, .Nombre = "Ana3", .Clase = "A"})
lst.Add(New User() With {.ID = 4, .Nombre = "Ana4", .Clase = "B"})
lst.Add(New User() With {.ID = 5, .Nombre = "Ana5", .Clase = "A"})
lst.Add(New User() With {.ID = 6, .Nombre = "Ana6", .Clase = "B"})
lst.Add(New User() With {.ID = 7, .Nombre = "Ana7", .Clase = "A"})
lst.Add(New User() With {.ID = 8, .Nombre = "Ana8", .Clase = "B"})
lst.Add(New User() With {.ID = 9, .Nombre = "Ana9", .Clase = "A"})
lst.Add(New User() With {.ID = 10, .Nombre = "Ana10", .Clase = "B"})
Dim frutas() As String = {"manzana", "plantano", "fresa", "pera", "melon", "sandia"}
Dim Precios() As Integer = {5, 6, 7, 8, 9, 10}
Dim objRet As User
Dim sRet As String
Dim iRet As String
Single
objRet = lst.Single(Function(x) x.ID = 1)
sRet = frutas.Single(Function(x) x = "pera")
iRet = Precios.Single(Function(x) x = 5)
Console.WriteLine(objRet.Nombre) ' Ana1
Console.WriteLine(sRet) 'pera
Console.WriteLine(iRet) '5
Try
' no existe - Exception La secuencia no contiene ningún elemento coincidente
objRet = lst.Single(Function(x) x.ID = 11)
sRet = frutas.Single(Function(x) x = "peras")
iRet = Precios.Single(Function(x) x = 4)
Catch ex As Exception
End Try
Try
objRet = lst.SingleOrDefault(Function(x) x.ID = 11) '=> nothing
sRet = frutas.SingleOrDefault(Function(x) x = "peras") '=> nothing
iRet = Precios.SingleOrDefault(Function(x) x = 4) ' => 0
Console.WriteLine(objRet.Nombre) ' Exception
Console.WriteLine(sRet) ' no falla porque imprime ''
Console.WriteLine(iRet) ' 0
Catch ex As Exception
End Try
Así que olvida el SingleOrDefault , FirstOrDefault , LastOrDefault porque los resultado serán los mismo
objRet = lst.Where(Function(x) x.ID = 11).DefaultIfEmpty(New User()).First() '=> Objeto
sRet = frutas.Where(Function(x) x = "peras").DefaultIfEmpty("Juan").First() '=> "Juan"
iRet = Precios.Where(Function(x) x = 4).DefaultIfEmpty(999).First() ' => 999
Console.WriteLine(objRet.Nombre) '
Console.WriteLine(sRet) ' Juan
Console.WriteLine(iRet) ' 999
Ahora otra curiosidad, que pasa cuando en una lista hacemos un group by. ¿Cómo se recorren los resultados?
Try
Dim lstAgrupado As IEnumerable(Of IGrouping(Of String, User)) = lst.GroupBy(Function(x) x.Clase)
Dim lstAgrupado2 = lst.GroupBy(Function(x) x.Clase)
' Quiero saber cuantos grupos me salieron
Console.WriteLine("Lista de grupos ")
For Each sgrupo In lstAgrupado
Console.WriteLine(sgrupo.Key)
Next
Console.WriteLine(" Fin: Lista de grupos")
'Quiero todos los valores ( vale es un tonteria que ya los tenia en la lista inicial)
Console.WriteLine("Lista de valores ")
For Each sgrupo In lstAgrupado.SelectMany(Function(x) x)
Console.WriteLine(sgrupo.Nombre)
Next
Console.WriteLine("Fin : Lista de valores ")
Console.WriteLine("Todo junto ")
For Each sgrupo In lstAgrupado
Console.WriteLine("Key:" & sgrupo.Key)
For Each sMigrupo In lstAgrupado.Single(Function(x) x.Key = sgrupo.Key)
Console.WriteLine(sMigrupo.Nombre)
Next
Console.WriteLine("Fin")
Next
Console.WriteLine("Fin: Todo junto")
Catch ex As Exception
End Try
Y adjunto la imagen de salida
No hay comentarios:
Publicar un comentario