LINQ示例
来源:互联网 发布:反恐精英起源 mac 编辑:程序博客网 时间:2024/06/08 09:05
Imports System.IO
Imports System.Reflection
Public Class Form1Class Form1
Public Sub Linq107()Sub Linq107()
Randomize()
Dim y = Year(Now)
Dim m = Month(Now)
'Randomly pick 15 appointment dates this month
Dim DentistDates(15) As Date, AvailableDates(8) As 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()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()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()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()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()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()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()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()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()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()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()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()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
Imports System.Reflection
Public Class Form1Class Form1
Public Sub Linq107()Sub Linq107()
Randomize()
Dim y = Year(Now)
Dim m = Month(Now)
'Randomly pick 15 appointment dates this month
Dim DentistDates(15) As Date, AvailableDates(8) As 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()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()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()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()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()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()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()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()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()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()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()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()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
- LINQ示例
- LINQ示例
- Linq示例
- Linq示例
- 简单linq示例
- LINQ to DataSet 示例
- LINQ 查询 示例
- LINQ 使用示例
- Linq where示例
- Linq:Select使用示例
- Linq常见操作示例
- LINQ基础_简单示例
- LINQ基本语法及其示例
- LINQ增删改代码示例
- Linq的常见查询语句示例
- linq版本E-Commerce网站示例
- MVC,Linq简单示例(一)
- Microsoft 微软101个LINQ查询示例
- oracle字段分组后字段拼接(转摘)
- 异步 SOCKET 编程 - 发送和接收数据
- 掌握.net 1.1配置文件的用法
- portlet 概念
- 菜鸟学PHP之Smarty入门
- LINQ示例
- 在网上找到一个解决MMS病毒方法,回去试验!!
- 编程修养
- IBM Portlet API
- prompt方法 window . prompt
- Effective C++ 学习笔记:让operator=返回*this的引用
- 如何正确地应用Runtime类调用程序
- Java applet(小程序)介绍
- IBM portlet API 开发