C#:linq返回boolean而不是string

时间:2016-02-17 19:59:05

标签: c# linq

我想从长度小于或等于5的列表中选择所有单词。我当前的代码只返回:

  1. true
  2. false
  3. false
  4. true
  5. true
  6. 我希望结果是真实的单词。

    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();
    }
    

1 个答案:

答案 0 :(得分:8)

看起来你打算做

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

或只是

var shortWords = words.Where(word => word.Length <= 5);