Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: linq return boolean instead of string

Tags:

c#

linq

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();
}
like image 433
Abdelrahman Hussien Avatar asked Mar 26 '26 04:03

Abdelrahman Hussien


1 Answers

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);
like image 104
juharr Avatar answered Mar 28 '26 17:03

juharr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!