EN
|
NL
jan 5

Written by: André Obelink (Host)
5-1-2009 22:32 

LINQ is fun. And LINQ is hot. You will find many resources writing about LINQ to XML, LINQ to SQL or LINQ to ET. That's great, but what your perhaps not know, is that you can query almost any resource on your machine with LINQ. The technology is called: LINQ to Objects. There are many possibilities to use LINQ. You can use it for querying running processes, directories, files, assemblies or even the registry. After you get used with the syntax of LINQ, you can try to query any collection that inherits directly or indirectly from IEnumerable(Of T). LINQ to Objects is powerful and very intuitive to use. I will demonstrate this with a couple of basic examples.

Query processes
This query will select the top 10 of the processes which consumes the highest memory. It will select the name of the process and the size in kilobytes.

   1: Dim processes = From process In System.Diagnostics.Process.GetProcesses() _
   2:                 Select ProcessName = process.ProcessName, _
   3:                        Size = ((process.WorkingSet64 / 1024) & " KB"), _
   4:                        WorkingSet64 = process.WorkingSet64 _
   5:                 Order By WorkingSet64 _
   6:                 Take 10
   7: 
   8: ' Show the top 10 of the processes which consumes the most memory 
   9: For Each process In processes
  10:     Console.WriteLine(process.ProcessName & " - " & process.Size)
  11: Next

Query drives
It's easy to determine all installed CD-ROM or DVD-drives on your system.

   1: Dim drives = From drive In My.Computer.FileSystem.Drives() _
   2:              Where drive.DriveType = System.IO.DriveType.CDRom _
   3:              Select drive
   4: 
   5: ' Show all installed CD-ROM drives
   6: For Each drive In drives
   7:     Console.WriteLine(drive.Name)
   8: Next

Query the registry
The query below will list al the sub keys under HKLM\Software starting with a 'M'. Quite intuitive, isn't?

   1: Dim keys = My.Computer.Registry.LocalMachine.OpenSubKey("Software").GetSubKeyNames
   2: 
   3: Dim subkeys = From subkey In keys _
   4:               Where subkey.StartsWith("M") _
   5:               Order By subkey
   6: 
   7: ' Show all names of subkeys starting with a 'M'
   8: For Each subkey In subkeys
   9:     Console.WriteLine(subkey)
  10: Next

Query Special Folders
You can use LINQ to Objects also for querying information about the Special Folders. This query will list all shortcuts in the Most Recent folder. The last used shortcut is listed first.

   1: ' Get the path to the Recent-files folder
   2: Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Recent)
   3: 
   4: Dim shortcuts = From shortcut In New System.IO.DirectoryInfo(path).GetFiles _
   5:                 Select Name = shortcut.Name, _
   6:                        LastAccessTime = shortcut.LastAccessTime _
   7:                 Order By LastAccessTime Descending
   8: 
   9: ' Show all shortcuts, the last used first
  10: For Each shortcut In shortcuts
  11:     Console.WriteLine(shortcut.Name & " - " & shortcut.LastAccessTime)
  12: Next

Query loaded assemblies
You want to know which assemblies are currently loaded? You can use LINQ. In this case I'm filtering on all the assemblies starting with 'Microsoft.' like Microsoft.VisualStudio.Debugger.Runtime or Microsoft.VisualBasic.

   1: Dim assemblies = From assembly In My.Application.Info.LoadedAssemblies _
   2:                  Where assembly.GetName().Name.StartsWith("Microsoft.") _
   3:                  Select assembly.GetName().Name
   4: 
   5: ' Show all loades assemblies starting with 'Microsoft.'
   6: For Each assembly In assemblies
   7:     Console.WriteLine(assembly)
   8: Next

If you want to know the number of loaded assemblies: use the Count() method. You can imagine that you can use this method and others like Max(), Average() etc. on almost every LINQ statement you will write.

   1: Dim count = (From assembly In My.Application.Info.LoadedAssemblies _
   2:              Where assembly.GetName().Name.StartsWith("System.") _
   3:              Select assembly.GetName().Name).Count()
   4: 
   5: ' Show all loades assemblies starting with 'System.'
   6: Console.WriteLine("There are {0} assemblies loaded starting with System.", count)

In my next blog post I will write about LINQ to DASL. With LINQ to DASL you can query e-mail, appointments etc. from Outlook.

Happy coding!

 

Tags:

1 comment(s) so far...

Re: VB2008 - Fun with LINQ - Part 1

In "Query processes" need 'Order By WorkingSet64 Descending'

By Alex on   29-1-2009 21:23

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Blog
Blog archive
 

Bataafseweg 20, 7101 PA  Winterswijk  |  PO box 428, 7100 AK Winterswijk Netherlands  |  T +31647914464  |  E info@maryor.nl