VB.Net/LINQ/Query

Материал из VB Эксперт
Перейти к: навигация, поиск

A select statement against object list

  
Imports System.Reflection
Imports System


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
Public Class Salary
    Public IDPerson As Integer
    Public Year As Integer
    Public SalaryYear As Double
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 salaries As New List(Of Salary)(New Salary() { _
                New Salary With {.IDPerson = 1, .Year = 2004, .SalaryYear = 10000.0}, _
                New Salary With {.IDPerson = 1, .Year = 2005, .SalaryYear = 15000.0}, _
                New Salary With {.IDPerson = 2, .Year = 2005, .SalaryYear = 15000.0} _
        })
        Dim query = From p In people Select p
    End Sub
End Module


Convert Linq result to Array

  
Option Explicit On
Option Strict On
Module Program
  Sub Main()
    Dim numbers() As Integer = {10, 20, 30, 40, 1, 2, 3, 8}
    Dim subsetAsIntArray() As Integer = (From i In numbers Where i < 10 Select i).ToArray()
  End Sub
End Module


Convert Linq result to List

  
Option Explicit On
Option Strict On
Module Program
  Sub Main()
    Dim numbers() As Integer = {10, 20, 30, 40, 1, 2, 3, 8}
    Dim subsetAsListOfInts As List(Of Integer) = (From i In numbers Where i < 10 Select i).ToList()
  End Sub
End Module


Convert query to a list of string

  
Imports System.Reflection
Imports System

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
Public Class Salary
    Public IDPerson As Integer
    Public Year As Integer
    Public SalaryYear As Double
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 salaries As New List(Of Salary)(New Salary() { _
                New Salary With {.IDPerson = 1, .Year = 2004, .SalaryYear = 10000.0}, _
                New Salary With {.IDPerson = 1, .Year = 2005, .SalaryYear = 15000.0}, _
                New Salary With {.IDPerson = 2, .Year = 2005, .SalaryYear = 15000.0} _
        })
        Dim query = From p In people _
            Where p.LastName.Length = 4 _
            Select p.LastName
        Dim names As List(Of String) = query.ToList()
    End Sub
End Module


Create a query expression

  
Imports System.Collections.Generic
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 fastCars = From c In myCars Where _
      c.Speed > 90 And c.Make = "BMW" Select c
    For Each car In fastCars
      Console.WriteLine("{0} is going too fast!", car.PetName)
    Next
  End Sub
End Module


Filter by name length

  
Imports System.Reflection
Imports System

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
Public Class Salary
    Public IDPerson As Integer
    Public Year As Integer
    Public SalaryYear As Double
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 salaries As New List(Of Salary)(New Salary() { _
                New Salary With {.IDPerson = 1, .Year = 2004, .SalaryYear = 10000.0}, _
                New Salary With {.IDPerson = 1, .Year = 2005, .SalaryYear = 15000.0}, _
                New Salary With {.IDPerson = 2, .Year = 2005, .SalaryYear = 15000.0} _
        })
        Dim query = From p In people _
            Where p.LastName.Length = 4 _
            Select p.LastName
        Dim names As String() = query.ToArray()
    End Sub
End Module


Find the cars that are going less than 55 mph, and order by descending PetName

  

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 subset = From c In myCars _
      Where c.Speed > 55 Order By c.PetName Descending Select c
    Console.WriteLine(vbLf + "Cars going faster than 55, ordered by PetName:")
    For Each c As Car In subset
      Console.WriteLine("Car {0}", c)
    Next
 
  End Sub
End Module


Get all cars. Similar to Select * in SQL

  
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 allCars = From c In myCars Select c
        For Each c In allCars
            Console.WriteLine("Car: {0}", c)
        Next
    End Sub
End Module


Get differences

  
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 myCarsName As String() = {"Y", "A", "B"}
        Dim yourCars As String() = {"B", "S", "A"}
        " Find the differences.
        Dim carDiff = (From c In myCarsName Select c) _
          .Except(From c2 In yourCars Select c2)
        For Each s As String In carDiff
            Console.WriteLine(s)
        Next

    End Sub
End Module


Get only the pet names

  
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 allNames = From c In myCars Select c.PetName
    For Each n In allNames
      Console.WriteLine("Pet Name: {0}", n)
    Next
  End Sub
End Module


Join Person and Salaries lists to get the max and min salary

  
Imports System.Reflection
Imports System

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
Public Class Salary
    Public IDPerson As Integer
    Public Year As Integer
    Public SalaryYear As Double
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 salaries As New List(Of Salary)(New Salary() { _
                New Salary With {.IDPerson = 1, .Year = 2004, .SalaryYear = 10000.0}, _
                New Salary With {.IDPerson = 1, .Year = 2005, .SalaryYear = 15000.0}, _
                New Salary With {.IDPerson = 2, .Year = 2005, .SalaryYear = 15000.0} _
        })
        
        Dim query = From p In people, s In salaries _
            Where p.ID = 1 _
            Select s.SalaryYear
        Console.WriteLine("Minimum Salary:")
        Console.WriteLine(query.Min())
        Console.WriteLine("Maximum Salary:")
        Console.WriteLine(query.Max())
    End Sub
End Module


Join two object lists together

  

Imports System.Reflection
Imports System

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
Public Class Salary
    Public IDPerson As Integer
    Public Year As Integer
    Public SalaryYear As Double
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 salaries As New List(Of Salary)(New Salary() { _
                New Salary With {.IDPerson = 1, .Year = 2004, .SalaryYear = 10000.0}, _
                New Salary With {.IDPerson = 1, .Year = 2005, .SalaryYear = 15000.0}, _
                New Salary With {.IDPerson = 2, .Year = 2005, .SalaryYear = 15000.0} _
        })
        Dim query = From p In people, r In roles _
            Where p.ID = 1 AndAlso r.ID = p.IDRole _
            Select New With {p.FirstName, p.LastName, r.RoleDescription}
    End Sub
End Module


Linq Over Array

  

Option Explicit On
Option Strict On
Module Program
    Sub Main()
        Dim currentVideoGames As String() = {"A", "B", "C", "this is a test", "D", "E"}
        Dim subset As IEnumerable(Of String) = From game In currentVideoGames Where game.Length > 6 Order By game Select game
        For Each s As String In subset
            Console.WriteLine("Item: {0}", s)
        Next
    End Sub
End Module


Linq Over int Array

  

Option Explicit On
Option Strict On
Module Program
  Sub Main()
    Dim numbers() As Integer = {10, 20, 30, 40, 1, 2, 3, 8}
    Dim subset = From i In numbers Where i < 10 Select i
    For Each i In subset
      Console.WriteLine("{0} < 10", i)
    Next
    numbers(0) = 4
    For Each i In subset
      Console.WriteLine("{0} < 10", i)
    Next
    Console.WriteLine(subset.GetType().Name)
    Console.WriteLine(subset.GetType().Assembly)
  End Sub
End Module


Query String Array With Operators

  
Module Program
    Sub Main()
        Dim currentVideoGames As String() = {"A", "B", "this is a test", "C", "D", "E"}
        Dim searchFilter As New Func(Of String, Boolean)(AddressOf Filter)
        Dim itemToProcess As New Func(Of String, String)(AddressOf ProcessItem)
        Dim subset = currentVideoGames.Where(searchFilter).OrderBy(itemToProcess).Select(itemToProcess)
        For Each game In subset
            Console.WriteLine("Item: {0}", game)
        Next
    End Sub
    Function Filter(ByVal str As String) As Boolean
        Return str.Length > 6
    End Function
    Function ProcessItem(ByVal str As String) As String
        Return str
    End Function
End Module


Reflector Linq result

  
Option Explicit On
Option Strict On
Module Program
  Sub Main()
    Dim numbers() As Integer = {10, 20, 30, 40, 1, 2, 3, 8}
    Dim subset = From i In numbers Where i < 10 Select i
    For Each i In subset
      Console.WriteLine("{0} < 10", i)
    Next
    numbers(0) = 4
    For Each i In subset
      Console.WriteLine("{0} < 10", i)
    Next
    Console.WriteLine(subset.GetType().Name)
    Console.WriteLine(subset.GetType().Assembly)
  End Sub
End Module


Select by a Property

  
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 onlyBMWs = From c In myCars Where c.Make = "BMW" Select c
    For Each n In onlyBMWs
      Console.WriteLine("Name: {0}", n)
    Next
  End Sub
End Module


Select by object id

  
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.rupilerServices
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 query = From p In people Where p.ID = 1 Select p

    End Sub

End Module


Using where clause to check the first name of a Persion

  
Imports System.Reflection
Imports System

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
Public Class Salary
    Public IDPerson As Integer
    Public Year As Integer
    Public SalaryYear As Double
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 salaries As New List(Of Salary)(New Salary() { _
                New Salary With {.IDPerson = 1, .Year = 2004, .SalaryYear = 10000.0}, _
                New Salary With {.IDPerson = 1, .Year = 2005, .SalaryYear = 15000.0}, _
                New Salary With {.IDPerson = 2, .Year = 2005, .SalaryYear = 15000.0} _
        })
        Dim query = From p In people Where p.FirstName = "B" Select p
    End Sub
End Module