LINQ示例

来源:互联网 发布:反恐精英起源 mac 编辑:程序博客网 时间:2024/06/08 09:05
Imports System.IO
Imports System.Reflection

Public Class Form1


    
Public Sub Linq107()
        
Randomize()

        
Dim y = Year(Now)
        
Dim m = Month(Now)

        
'Randomly pick 15 appointment dates this month
        Dim DentistDates(15As Date, AvailableDates(8As Date
        
For i = 0 To UBound(DentistDates)
            DentistDates(i) 
= New Date(y, m, Int(Date.DaysInMonth(y, m) * Rnd() + 1))
        
Next

        
'Randomly pick 8 dates you're free this month
        For i = 0 To UBound(AvailableDates)
            AvailableDates(i) 
= New Date(y, m, Int(Date.DaysInMonth(y, m) * Rnd() + 1))
        
Next

        
Dim CommonDates = From _
                 d 
In AvailableDates, d2 In DentistDates _
            Where d.Date 
= d2.Date _
            
Select d _
            Distinct _
            Order By d

        
'Just to make it easier to see that the query works, we sort the original lists before display
        Array.Sort(DentistDates)
        Array.Sort(AvailableDates)

        Console.WriteLine(
"Dentist is free on:")
        Console.Write(DentistDates)

        Console.WriteLine(vbNewLine 
& "You are free on:")
        Console.Write(AvailableDates)

        Console.WriteLine(vbNewLine 
& "Appointment dates that work:")
        
For Each d In CommonDates
            Console.WriteLine(
Format(d, "MM/dd/yy"))
        
Next
    
End Sub



    
Public Sub Linq108()

        
Dim pList = From p In System.Diagnostics.Process.GetProcesses() _
                    
Select ProcessName = p.ProcessName, _
                           Size 
= (Format(p.WorkingSet64 / 1000"#,##0"& " KB"), _
                           Size64 
= p.WorkingSet64 _
                    Order By Size64 Take 
5

        Console.WriteLine(
"These 5 processes are using the most memory:")

        
For Each p In pList
            Console.WriteLine(p.ProcessName 
& " - " & p.Size)
        
Next

    
End Sub


    
Public Sub Linq109()

        
'只隐藏,不存档的文件 
        Dim fileList = From f In New DirectoryInfo("C:").GetFiles() _
            Where ((f.Attributes 
And FileAttributes.Hidden = FileAttributes.Hidden) And ((f.Attributes And FileAttributes.Archive) <> FileAttributes.Archive)) _
            Order By f.Name

        Console.WriteLine(
"Hidden Files:")

        
For Each f In fileList
            Console.WriteLine(f.Name)
        
Next
    
End Sub


    
Public Sub Linq110()

        
Dim fileList = From f In New DirectoryInfo("C:").GetFiles() _
                       Where f.CreationTime.AddYears(
1> Now _
                       
Select f _
                       Order By f.Name

        Console.WriteLine(
"Files created within the last year:")
        
For Each f In fileList
            Console.WriteLine(f.Name)
        
Next

    
End Sub


    
Public Sub Linq111()

        
'If you want to see other drive types (i.e. CDRom) please change the enum below
        Dim Drives = From d In My.Computer.FileSystem.Drives() _
                     Where d.DriveType 
= System.IO.DriveType.Network _
                     
Select d

        Console.WriteLine(
"Network Drives:")
        
For Each d In Drives
            Console.WriteLine(d.Name)
        
Next

    
End Sub


    
Public Sub Linq112()
        
'To experiment with this you can double-click My Project, click on Debug and
        'enter /help in the command line arguments textbox

        
Dim cmdArgs = (From c In My.Application.CommandLineArgs() _
                      Where c 
= "/help" _
                      
Select c).Count()

        
If cmdArgs = 0 Then
            Console.WriteLine(
"""/help"" was not entered as a command line argument.")
        
Else
            Console.WriteLine(
"""/help"" requested by user on the command line.")
        
End If

    
End Sub



    
Public Sub Linq113()

        
Dim SubKeys = _
            My.Computer.Registry.LocalMachine.OpenSubKey(
"Software").GetSubKeyNames

        
Dim reg = From r In SubKeys _
                  Where r.StartsWith(
"C") _
                  Order By r

        Console.Write(reg)
    
End Sub




    
Public Sub Linq114()

        
Dim LMKeys = _
            My.Computer.Registry.LocalMachine.OpenSubKey(
"Software").GetSubKeyNames

        
Dim UserKeys = _
            My.Computer.Registry.CurrentUser.OpenSubKey(
"Software").GetSubKeyNames

        
'Performs an intersection on the two arrays
        Dim reg = From LocalMachineKey In LMKeys, UserKey In UserKeys _
                  Where LocalMachineKey 
= UserKey _
                  
Select LocalMachineKey, UserKey _
                  Order By LocalMachineKey

        Console.WriteLine(
"Keys common to HKLMSoftware and HKCUSoftware:")

        
For Each r In reg
            Console.WriteLine(r.LocalMachineKey)
        
Next
    
End Sub



    
Public Sub Linq115()

        
Dim RecentPath As String = _
            Environment.GetFolderPath(Environment.SpecialFolder.Recent)

        
'Uses an anonymous type to reduce the amount of information returned
        Dim Recent = From r In New DirectoryInfo(RecentPath).GetFiles _
                     
Select r.Name, r.LastAccessTime

        Console.WriteLine(
"Shortcuts to My Recent Documents:")

        
For Each r In Recent
            Console.WriteLine(
"{0,-45}{1}", r.Name, r.LastAccessTime)
        
Next
    
End Sub



    
Public Sub Linq116()

        
Dim FavPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

        
Dim FavSize = (From f In New DirectoryInfo(FavPath).GetFiles() _
                       
Select f).Count()

        Console.WriteLine(
"There are {0} files in your Favorites folder.", FavSize)
    
End Sub



    
Public Sub Linq117()

        
Dim Assemblies = From a In My.Application.Info.LoadedAssemblies _
                         Where a.GetName().Name.StartsWith(
"System.") _
                         
Select a.GetName().Name

        Console.WriteLine(
"Currently-loaded System assemblies:")
        
For Each a In Assemblies
            Console.WriteLine(a)
        
Next
    
End Sub



    
Public Sub Linq118()

        
Dim MethodList = From _
                 t 
In Assembly.GetExecutingAssembly.GetTypes(), _
                 m 
In t.GetMembers() _
            Where m.MemberType 
= MemberTypes.Method _
                
AndAlso CType(m, MethodInfo).IsPublic _
            
Select Item = CType(m, MethodInfo) _
            Order By Item.Name

        
For Each m In MethodList
            Console.WriteLine(m.Name)
        
Next
    
End Sub



    
Public Sub Linq119()

        
Dim NameList = From m In _
                (From t 
In _
                    
Assembly.GetExecutingAssembly.GetTypes(), _
                      m 
In t.GetMembers() _
            Where m.MemberType 
= MemberTypes.Method AndAlso CType(m, MethodInfo).IsPublic _
            
Select Item = CType(m, MethodInfo) _
            Order By Item.Name) _
            
Select m.Name _
            Distinct

        
For Each m In NameList
            Console.WriteLine(m)
        
Next

    
End Sub


End Class

 
原创粉丝点击