3

I want to select all the words from a list whose length is less or equal to 5. My current code only returns this:

  1. true
  2. false
  3. false
  4. true
  5. true
  6. true

I want the result to be the actual words.

static void Main()
{
    string[] words = { "hello", "Welcome", "Rolling", "in", "The", "Deep" };
    var shortWords = from word in words select word.Length <= 5;

    foreach (var word in shortWords) {
        Console.WriteLine(word);
    }

    Console.Read();
}
0

1 Answer 1

8

Looks like you meant to do

var shortWords = from word in words where word.Length <= 5 select word;

or just

var shortWords = words.Where(word => word.Length <= 5);
Sign up to request clarification or add additional context in comments.

1 Comment

It's worth marking this as the answer if it assisted you, so that people are not hesitant to answer your future questions.

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.