Hooked on LINQ

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

Quick Search

Advanced Search »

Where Operator

Modified: 2007/06/22 23:57 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 Where operator filters a sequence based on a predicate.

Edit

Method Signatures

// 1 - Only return elements that pass the filter predicate.
public static IEnumerable<TSource> Where<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate)
 
// 2 - Only return elements that pass a filter predicate.
// The current element's index position is available for use in the predicate function.
public static IEnumerable<TSource> Where<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, int, bool> predicate)



Edit

Exceptions

Throws an ArgumentNullException if source or predicate is null.


Edit

Pseudo-code

Overload 1
If source is null, throw an ArgumentNullException.
If predicate is null, throw an ArgumentNullException.
Iterate the source sequence.
If predicate(current element) returns true.
Return the current element. Resume execution from this point when the next element is requested.

Overload 2
If source is null, throw an ArgumentNullException.
If predicate is null, throw an ArgumentNullException.
Initialize a counter to zero.
Iterate the source sequence.
If predicate(current element, counter) returns true.
Return the current element. Resume execution from this point when the next element is requested.
Increment counter by one.

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 Restriction

 
       Dim num() = {5,1, 3, 9, 8, 6, 7, 2}
       Dim numLess = From n In num _
                     Where n < 5 _
                     Select n
 
       Console.WriteLine("Numbers < 5:")
       For Each x In numLess
           Console.WriteLine(x)
       Next
 

Numbers < 5:
1
3
2

Alternate syntax,
 
       Dim num() = {5,1, 3, 9, 8, 6, 7, 2}
       Dim numLess = From n In num.Where(Function(w) w < 5).Select(Function(s) s)
 
       Console.WriteLine("Numbers < 5:")
       For Each x In numLess
           Console.WriteLine(x)
       Next
 



The following query finds hidden files in a given directory,

 
    Dim fileList = From f In New DirectoryInfo("C:\").GetFiles() _
                   Where (f.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden _
                   Select f _
                   Order By f.Name
 
    Console.WriteLine("Hidden Files:")
 
    For Each f In fileList
        Console.WriteLine(f.Name)
    Next



Results may vary.
Hidden Files:
bootmgr

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 ommissions 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 un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.