PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/VBLinqToObject/MainModule.vb

#
Visual Basic | 114 lines | 45 code | 32 blank | 37 comment | 0 complexity | 9e04d94133fd9e367a7c1a59c9b7f261 MD5 | raw file
  1. '****************************** Module Header ******************************\
  2. ' Module Name: Program.cs
  3. ' Project: VBLinqToObject
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' This example illustrates how to write Linq to Object queries using Visual
  7. ' VB.NET. First, it builds a class named Person. Person inculdes the ID,
  8. ' Name and Age properties. Then the example creates a list of Person which
  9. ' will be used as the datasource. In the example, you will see the basic
  10. ' Linq operations like select, update, orderby, max, average, etc.
  11. '
  12. ' This source is subject to the Microsoft Public License.
  13. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  14. ' All other rights reserved.
  15. '
  16. ' History:
  17. ' * 5/8/2009 02:00 PM Lingzhi Sun Created
  18. '***************************************************************************/
  19. Module MainModule
  20. Sub Main()
  21. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  22. ' Build the Person list that serves as the data source.
  23. '
  24. Dim persons As New List(Of Person)
  25. persons.Add(New Person(1, "Ereka", 20))
  26. persons.Add(New Person(2, "Marion", 18))
  27. persons.Add(New Person(3, "Grace", 21))
  28. persons.Add(New Person(4, "David", 19))
  29. persons.Add(New Person(5, "Michael", 20))
  30. persons.Add(New Person(6, "Stephen", 21))
  31. persons.Add(New Person(7, "Sam", 22))
  32. persons.Add(New Person(8, "Cael", 19))
  33. persons.Add(New Person(9, "Eric", 20))
  34. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  35. ' Query a person in the data source.
  36. '
  37. Dim ereka = (From p In persons _
  38. Where p.Name = "Ereka" _
  39. Select p).First()
  40. Console.WriteLine("Ereka's age is {0}", ereka.Age)
  41. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  42. ' Perform the Update operation on the person's age.
  43. '
  44. ereka.Age = 21
  45. Console.WriteLine("Ereka's age is updated to 21")
  46. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  47. ' Sort the data in the data source.
  48. '
  49. ' Order the persons by age
  50. Dim query1 = From p In persons _
  51. Order By p.Age _
  52. Select p
  53. Console.WriteLine("ID" & vbTab & "Name" & vbTab & vbTab & "Age")
  54. For Each p In query1.ToList()
  55. Console.WriteLine("{0}" & vbTab & "{1}" & vbTab & vbTab & "{2}", _
  56. p.PersonID, p.Name, p.Age)
  57. Next
  58. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  59. ' Print the average, max, min age of the persons.
  60. '
  61. Dim avgAge As Double = (From p In persons _
  62. Select p.Age).Average()
  63. Console.WriteLine("The average age of the persons is {0:f2}", avgAge)
  64. Dim maxAge As Double = (From p In persons _
  65. Select p.Age).Max()
  66. Console.WriteLine("The maximum age of the persons is {0}", maxAge)
  67. Dim minAge As Double = (From p In persons _
  68. Select p.Age).Min()
  69. Console.WriteLine("The minimum age of the persons is {0}", minAge)
  70. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  71. ' Count the persons who age is larger than 20.
  72. '
  73. Dim query2 = From p In persons _
  74. Where p.Age > 20 _
  75. Select p
  76. Dim count As Integer = query2.Count()
  77. Console.WriteLine("{0} persons are older than 20:", count)
  78. For i = 0 To count - 1
  79. Console.WriteLine(query2.ElementAt(i).Name)
  80. Next
  81. End Sub
  82. End Module