Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

Select Operator

Modified: 2007/06/23 00:39 by msft_binyam - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The Select operator performs a projection over a sequence.

Edit

Method Signatures

// 1 - Project elements of the source into a new form.
public static IEnumerable<TResult> Select<TSource, TResult>(
    this IEnumerable<TSource> source, 
    Func<TSource, TResult> selector)
 
// 2 - Project elements of the source into a new form. 
// Also provides index position as an argument for use in the selector function.
public static IEnumerable<TResult> Select<TSource, TResult>(
    this IEnumerable<TSource> source, 
    Func<TSource, int, TResult> selector)



Edit

Exceptions

Throws an ArgumentNullException if source or selector are null.


Edit

Pseudo-code

Overload 1
If source is null, throw an ArgumentNullException.
If selector is null, throw an ArgumentNullException.
Iterate through the source elements.
Return the result of the function selector(current element). Resume execution from this point when the next element is requested.

Overload 2
If source is null, throw an ArgumentNullException.
If selector is null, throw an ArgumentNullException.
Initialize a counter value to -1.
Iterate through the source elements.
Increment the counter by 1.
Return the result of the function selector(current element, counter). Resume execution from this point when the next element is requested.

Note: The index value that is passed in as an argument is zero-based. That means the first element will have an index of 0, the second will have an index of 1, etc.


Edit

Loop Count

This operator implements the standard deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.

Edit

Code Samples

==VB Simple Projection==

Public Sub Linq()
   Dim numbers() = {5, 4, 1, 3}
 
   Dim numsPlusOne = From n In numbers _
                     Select n + 1
 
 
   Console.WriteLine("Numbers + 1:")
 
   For Each i In numsPlusOne
       Console.WriteLine(i)
   Next
End Sub

Numbers + 1:
6
5
2
4

Edit

VB Select Projection return anonymous type

Public Sub Linq()
   Dim words() = {"aPPLE", "BlUeBeRrY", "cHeRry"}
 
   Dim upperLower = From w In words _
                    Select New With {.Upper = w.ToUpper(), .Lower = w.ToLower()}
 
   For Each ul In upperLower
       Console.WriteLine("Uppercase: " & ul.Upper & ", Lowercase: " & ul.Lower)
   Next
End Sub

Uppercase: APPLE, Lowercase: apple
Uppercase: BLUEBERRY, Lowercase: blueberry
Uppercase: CHERRY, Lowercase: cherry

Edit

VB Select projection determines the top 5 memory-using applications currently loaded

Public Sub Linq()
 
    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



Result may vary,
These 5 processes are using the most memory:
Idle - 16 KB
smss - 94 KB
wininit - 238 KB
sqlwriter - 238 KB
InoRpc - 340 KB

Edit

VB Select projection Shows keys common to HKLM\Software and HKCU\Software

Public Sub Linq()
 
    Dim LocMachineKeys = _
       My.Computer.Registry.LocalMachine.OpenSubKey("Software").GetSubKeyNames
 
    Dim uKeys = _
       My.Computer.Registry.CurrentUser.OpenSubKey("Software").GetSubKeyNames
 
    'Performs an intersection on the two arrays
    Dim common = From LocMachineKey In LocMachineKeys, uKey In uKeys _
                 Where LocMachineKey = uKey _
                 Select LocMachineKey, uKey _
           
    Console.WriteLine("Keys common to HKLM\Software and HKCU\Software:")
 
    For Each c In common
        Console.WriteLine(c.LocMachineKey)
    Next
End Sub



Alternate Syntax,
 
  Dim common2 = (From LocMachineKey In LocMachineKeys, uKey In uKeys _
                 Where LocMachineKey = uKey).Select(Function(c) c)
 
 

Result may vary,
Keys common to HKLM\Software and HKCU\Software:
Classes
ComputerAssociates
Macromedia
Microsoft
ODBC
Policies

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.