2

Please look at following code in Linqpad and tell me why it returns 0 items instead of 1 items.

void Main()
{
    string[] strArray = {"apple", "banana", "cherry", "e"};
    List<string> lst = strArray.ToList();

    //count all occurences of variable alphabet "e" in LINQ

    //tip is to get the occurences of letter "e" in each word
    // and then total them together

    var lst2 = lst.TakeWhile(c=>c.Equals("banana")).Select(c=>c);

    Console.WriteLine(lst2);
}

The above code does not return 1 item in linqpad as I would expect. Instead it returns 0 items. The list with 1 item "banana" should return. Why does it not?

5
  • use Where instead of takewhile Commented Aug 7, 2015 at 15:46
  • 6
    Looking at the documentation, it seems pretty clear to me. Why did you expect it to return 1 item, bearing in mind what the documentation says? Commented Aug 7, 2015 at 15:49
  • Why the down vote? This is a legit question and before reading it I had never looked at TakeWhile. Thanks for asking user1141419. Commented Aug 7, 2015 at 15:51
  • 1
    It's take while, not take until. Maybe you actually want lst.TakeWhile(c=>c!="banana") Commented Aug 7, 2015 at 15:54
  • 2
    @Sam: I downvoted because it at least appears to show a lack of research: reading the documentation is all that's required to answer the question. If the OP has actually read the documentation but is still surprised by the result, they should have explained why they expected there to be a single result. Questions which are explained just by reading simple documentation aren't useful, IMO. (If the documentation is unclear or complicated, that's a different matter - but I don't believe it's the case here.) Commented Aug 7, 2015 at 16:00

6 Answers 6

13

Documentation for TakeWhile:

Returns elements from a sequence as long as a specified condition is true.

Since a List is ordered, the first item, "apple" does not equal "banana". The condition is false and TakeWhile exits before it reaches the "banana" item.

You may be looking to use the Where method instead

Filters a sequence of values based on a predicate.

Sign up to request clarification or add additional context in comments.

Comments

4

@arcyqwerty explained why you're getting your results. For your expected results, use Where in this instance:

var lst2 = lst.Where(c=>c.Equals("banana"));

Also, no need for Select(c => c), it's redundant.

1 Comment

Humpf... Beat me by 32 seconds. Knew I should not have actually bothered testing it....
2

Because it is taking while the iterated item is a banana, since the first item it arrives to is not a banana, it stops the iteration.

Comments

1

The TakeWhile will take element as long as the condition is true. In your case it's false at the beginning because it's evaluate if ("apple" == "banana") and it's not so the TakeWhile stop.

If you put the element "banana" at beginning, it will work.

string[] strArray = {"banana", "apple", "cherry", "e"};

Plus, you can only write.

 var lst2 = lst.TakeWhile(c=>c.Equals("banana"))

The select is useless.

Comments

1

This will return 1 item ("banana"):

var result = lst.Where(c => c.Equals("banana")).ToList();
Console.WriteLine(result);

As the others pointed out, no need for a TakeWhile where a simple Where will suffice.

EDIT: From your code comments, it looks like you might be trying to count the occurrences of 'e' in the source list. This will do that for you:

var list = new List<string> { "apple", "banana", "cherry", "e" };

var count = list
    .SelectMany(x => x.ToArray()) // flatten into list of chars
    .Where(x => x.Equals('e'))
    .Count();
Console.Write(count);

Comments

0

arcyqwerty tell you why. He left out what you want instead:

var lst2 = lst.Where(c=>c.Equals("banana")).Select(c=>c);

1 Comment

Select(c=>c) is unnecessary, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.