Posted by: Cirilo Meggiolaro | 02/6/2009

Tip of the day #115 – TakeWhile extension method – Enumerable class

The TakeWhile extension method from the Enumerable class is used to retrieve elements in a collection while a condition is true. It means that the extension method checks one by one the items in the collection based on a specific Boolean expression until an item that does not match the condition has been found. At that point the method stops to check and retrieves all the items that match the condition so far.

Definition

The TakeWhile extension method has the following signature overloads available:

The first overload takes a Boolean expression to be evaluated:

public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

The following overload takes a Boolean expression to be evaluated but also the item index as parameter:

public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

How to…

Using the following class as our custom object:

public class Order
{
    public int OrderID { get; set; }
    public int CustomerID { get; set; }
    public DateTime OrderDate { get; set; }
    public decimal BilledTotal { get; set; }
}

We have the following self-explanatory code:

/// Creates a list of orders.
List<Order> orders = new List<Order>()
{
    new Order() { OrderID = 1, CustomerID = 1, OrderDate = DateTime.Now, BilledTotal = 1234.56M },
    new Order() { OrderID = 2, CustomerID = 2, OrderDate = DateTime.Now, BilledTotal = 46.99M },
    new Order() { OrderID = 3, CustomerID = 2, OrderDate = DateTime.Now, BilledTotal = 999.00M },
    new Order() { OrderID = 4, CustomerID = 1, OrderDate = DateTime.Now, BilledTotal = 78.99M },
    new Order() { OrderID = 5, CustomerID = 1, OrderDate = DateTime.Now, BilledTotal = 8.99M },
    new Order() { OrderID = 6, CustomerID = 3, OrderDate = DateTime.Now, BilledTotal = 15.38M }
};

/// Creates an IEnumerable object that retrieves Order objects
/// that have BilledTotal values greater than 10. Once the first

/// expression evaluation is false it stops returning items.
IEnumerable<Order> greaterThanTen = orders.TakeWhile(o => o.BilledTotal > 10);

/// Loops through the results.
foreach (Order customerOrder in greaterThanTen)
{
    /// Displays the order details.
    Console.WriteLine(“Customer ID: {0}, Order ID: {1}, Order Date: {2}, Total Value: {3}”, customerOrder.CustomerID, customerOrder.OrderID, customerOrder.OrderDate, customerOrder.BilledTotal);
}

The code above is just for this example purpose. You may call the Take extension method directly from the foreach loop:

foreach (Order customerOrder in orders.TakeWhile(o => o.BilledTotal > 10))
{ }

Output

Customer ID: 1, Order ID: 1, Order Date: 2/4/2009 10:53:50pm, Total Value: 1234.56
Customer ID: 2, Order ID: 2, Order Date: 2/4/2009 10:53:50pm, Total Value: 46.99
Customer ID: 2, Order ID: 3, Order Date: 2/4/2009 10:53:50pm, Total Value: 999.00
Customer ID: 1, Order ID: 4, Order Date: 2/4/2009 10:53:50pm, Total Value: 78.99

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: