VB.Net/LINQ/Anonymous Object — различия между версиями
Admin (обсуждение | вклад) м (1 версия)  | 
				|
(нет различий) 
 | |
Версия 16:40, 26 мая 2010
Содержание
- 1 An anonymous type is returned containing the Id, Name and Pay
 - 2 Calls ToString() on each anonymous object
 - 3 Create anonymous type from query
 - 4 Create anonymous type in query and convert to array
 - 5 Get structured data which only accounts for the Make and Color of each item
 - 6 Linq way of doing Object creation
 
An anonymous type is returned containing the Id, Name and Pay
  
Imports System
Imports System.Xml.Linq
    Public Class MainClass
        Public Shared Sub Main()
            Dim employees As XElement = XElement.Load("Employees.xml")
            Dim query = From ele In employees.<Employee> _
                        Where CDbl(ele.<HourlyRate>.Value) >= 100 _
                        Select ele.@id, ele.<Name>.Value, Pay = CDbl(ele.<HourlyRate>.Value) _
                        Order By Name
            For Each emp In query
                Console.WriteLine("[{0,-2}] {1,-25} ${2,-6}", emp.id, emp.Name, emp.Pay.ToString("##.00"))
            Next
        End Sub
    End Class
Calls ToString() on each anonymous object
  
Public Class Car
  Public PetName As String = String.Empty
  Public Color As String = String.Empty
  Public Speed As Integer
  Public Make As String = String.Empty
  Public Overloads Overrides Function ToString() As String
    Return String.Format("Make={0}, Color={1}, Speed={2}, PetName={3}", _
      Make, Color, Speed, PetName)
  End Function
End Class
Module Program
  Sub Main()
        Dim myCars As New List(Of Car)(New Car() { _
    New Car With {.PetName = "D", .Color = "Silver", .Speed = 100, .Make = "BMW"}, _
    New Car With {.PetName = "C", .Color = "Tan", .Speed = 90, .Make = "BMW"}, _
    New Car With {.PetName = "B", .Color = "Rust", .Speed = 5, .Make = "Yugo"}, _
    New Car With {.PetName = "A", .Color = "White", .Speed = 43, .Make = "Ford"}})
    Dim makesColors = From c In myCars Select New With {c.Make, c.Color}
    Dim objs As Array = makesColors.ToArray()
    For Each o As Object In objs
      Console.WriteLine(o)  " .
    Next
  End Sub
End Module
Create anonymous type from query
  
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.rupilerServices
Public Class Role
    Public ID As Integer
    Public RoleDescription As String
End Class
Public Class Person
    Public ID As Integer
    Public IDRole As Integer
    Public LastName As String
    Public FirstName As String
End Class
Module Module1
    Sub Main()
        Dim people As New List(Of Person)(New Person() { _
            New Person With {.ID = 1, .IDRole = 1, .LastName = "A", .FirstName = "Brad"}, _
            New Person With {.ID = 2, .IDRole = 2, .LastName = "G", .FirstName = "Tom"} _
        })
        Dim roles As New List(Of Role)(New Role() { _
            New Role With {.ID = 1, .RoleDescription = "Manager"}, _
            New Role With {.ID = 2, .RoleDescription = "Developer"} _
        })
        Dim query = From p In people Where p.ID = 1 Select New With {p.FirstName, p.LastName}
    End Sub
End Module
Create anonymous type in query and convert to array
  
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.rupilerServices
Public Class Role
    Public ID As Integer
    Public RoleDescription As String
End Class
Public Class Person
    Public ID As Integer
    Public IDRole As Integer
    Public LastName As String
    Public FirstName As String
End Class
Module Module1
    Sub Main()
        Dim people As New List(Of Person)(New Person() { _
            New Person With {.ID = 1, .IDRole = 1, .LastName = "A", .FirstName = "Brad"}, _
            New Person With {.ID = 2, .IDRole = 2, .LastName = "G", .FirstName = "Tom"} _
        })
        Dim roles As New List(Of Role)(New Role() { _
            New Role With {.ID = 1, .RoleDescription = "Manager"}, _
            New Role With {.ID = 2, .RoleDescription = "Developer"} _
        })
        Dim query = (From p In people Where p.ID = 1 Select New With {p.FirstName, p.LastName}).ToArray()
        people(0).FirstName = "Fabio"
    End Sub
End Module
Get structured data which only accounts for the Make and Color of each item
  
Public Class Car
  Public PetName As String = String.Empty
  Public Color As String = String.Empty
  Public Speed As Integer
  Public Make As String = String.Empty
  Public Overloads Overrides Function ToString() As String
    Return String.Format("Make={0}, Color={1}, Speed={2}, PetName={3}", Make, Color, Speed, PetName)
  End Function
End Class
Module Program
  Sub Main()
        Dim myCars As New List(Of Car)(New Car() { _
    New Car With {.PetName = "D", .Color = "Silver", .Speed = 100, .Make = "BMW"}, _
    New Car With {.PetName = "C", .Color = "Tan", .Speed = 90, .Make = "BMW"}, _
    New Car With {.PetName = "B", .Color = "Rust", .Speed = 5, .Make = "Yugo"}, _
    New Car With {.PetName = "A", .Color = "White", .Speed = 43, .Make = "Ford"}})
    Dim makesColors = From c In myCars Select New With {c.Make, c.Color}
    For Each n In makesColors
      Console.WriteLine("Name: {0}", n)
    Next
 
  End Sub
End Module
Linq way of doing Object creation
  
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.rupilerServices
Public Class Role
    Public ID As Integer
    Public RoleDescription As String
End Class
Public Class Person
    Public ID As Integer
    Public IDRole As Integer
    Public LastName As String
    Public FirstName As String
End Class
Module Module1
    Sub Main()
        Dim people As New List(Of Person)(New Person() { _
            New Person With {.ID = 1, .IDRole = 1, .LastName = "A", .FirstName = "Brad"}, _
            New Person With {.ID = 2, .IDRole = 2, .LastName = "G", .FirstName = "Tom"} _
        })
        Dim roles As New List(Of Role)(New Role() { _
            New Role With {.ID = 1, .RoleDescription = "Manager"}, _
            New Role With {.ID = 2, .RoleDescription = "Developer"} _
        })
        Dim p2 = New Person With {.FirstName = "Tom", .LastName = "Gray"}
    End Sub
End Module