{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The Select operator performs a projection over a sequence.
EditMethod 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)EditExceptions
Throws an ArgumentNullException if
source or
selector are null.
EditPseudo-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.
EditLoop Count
This operator implements the standard
deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.
EditCode 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
EditVB 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
EditVB 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 SubResult 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
EditVB 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 SubAlternate 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