none
what is the difference between .TakeWhile and .Where in Linq?

    Question

  • I have this numbers array

    int[] numbers = {5,4,1,3,9,8,6,7,2,0};

    I perform the following Linq operations using numbers.TakeWhile(...) and numbers.Where(...) which both operations return the same results -- 5, 4.

    var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
    foreach (int i in firstSmallNumbers)
    {
       Console.WriteLine(i.ToString());
    }
    Console.WriteLine();

    var NextSmallNumbers = numbers.Where((n, index) => n >= index);
    foreach (int i in firstSmallNumbers)
    {
       Console.WriteLine(i.ToString());
    }


    What is the difference between .TakeWhile and .Where?  What would be a an example where there operations would return different results on the same data?

    Thanks


    Rich P

    Thursday, February 28, 2013 10:31 PM

Answers

  • Where filters the input sequence, including all items where predicate [for that item] returns true

    TakeWhile iterates through the input sequence, yielding items until predicate [for that item] doesn't return true.

    simplistic "metaphoric" example:

    you let people enter a dancefloor if they have the money so you'll use a Where.

    var peopleInDancefloor = allPeople.Where(p => p.Money >= 42);

    you let people enter a dancefloor until full capacity reached, you'll use a TakeWhile.

    int capacity = 100;
    // not the best example 'cause it counts on side effect
    var peopleInDancefloor = allPeople.TakeWhile(p => --capacity > 0);

    If you want to see the difference with your starting input, simply make the last item superior or equal to it's index.

    more specifically you just have to have one item for which predicate is false and then other for which it's true

    In this case TakeWhile will return the same result but Where will include the additional items past the one which returned false.

    • Marked as answer by Rich P123 Thursday, February 28, 2013 11:08 PM
    Thursday, February 28, 2013 10:56 PM

All replies

  • Where filters the input sequence, including all items where predicate [for that item] returns true

    TakeWhile iterates through the input sequence, yielding items until predicate [for that item] doesn't return true.

    simplistic "metaphoric" example:

    you let people enter a dancefloor if they have the money so you'll use a Where.

    var peopleInDancefloor = allPeople.Where(p => p.Money >= 42);

    you let people enter a dancefloor until full capacity reached, you'll use a TakeWhile.

    int capacity = 100;
    // not the best example 'cause it counts on side effect
    var peopleInDancefloor = allPeople.TakeWhile(p => --capacity > 0);

    If you want to see the difference with your starting input, simply make the last item superior or equal to it's index.

    more specifically you just have to have one item for which predicate is false and then other for which it's true

    In this case TakeWhile will return the same result but Where will include the additional items past the one which returned false.

    • Marked as answer by Rich P123 Thursday, February 28, 2013 11:08 PM
    Thursday, February 28, 2013 10:56 PM
  • I know this is marked as answered but I thought I should point something out.

    Your two examples are cases where TakeWhile and Where give difference results.  You see the same result because your foreach loops which display results both iterate over firstSmallNumbers.  If you make the second foreach iterate over NextSmallNumbers you would see that your two examples are very different.

    By the way, you don't need .ToString() inside your call to WriteLine.  Console.WriteLine(i); works just fine.


    Paul Linton

    Friday, March 01, 2013 4:47 AM