2016-05-12 4 views
1

ほとんどの文字で単語をリストしようとしています。私はLINQの新人ですし、ちょっと混乱しています。あなたはOrderByDescendingを使用し、直接使用することができlinqを使って必要な文字で配列から単語を表示

string sentence = "write wwww five cat com LINQ queries to do the following good abba"; 
string[] words = sentence.Split(' '); 

IEnumerable<string> query3 = words 
      .Where(n => n)   
      .OrderBy(n.Length).Reverse; 

     IEnumerable<string> query33 = query3 
      .Where(n => n.First.length) 
+3

'' words.OrderByDescending(X => x.Trim()の長さ。) '' –

+0

そして、どのように:しかし、私はMaxTakeWhileを使用することになり、あなたは順序付けられたシーケンスから継続したいとします最初の単語と同じ長さの単語をすべて選択できます – coco

答えて

3

は、これは私のコードです

string sentence = "write wwww five cat com LINQ queries to do the following good abba"; 
string[] words = sentence.Split(' '); 
IEnumerable<string> query3 = words 
        .OrderByDescending(n => n.Length); 

そして、あなたは、2番目のクエリ、最初の1(query3)を必要としません。

OrderByDescendingは、IEnumerableの降順での注文方法を決定するためにラムダパラメータを受け取ります。順序のパラメータとしてIEnumerableの文字列のLengthを入力するだけです。

更新:

(これはコメントではなく、質問に基づいています)

あなたが最初のものと同じ長さですべての単語を取りたい場合は、あなたには、いくつかを持っています実際の選択肢。

string sentence = "write wwww five cat com LINQ queries to do the following good abba"; 
string[] words = sentence.Split(' '); 
IEnumerable<string> query3 = words 
        .OrderByDescending(n => n.Length); 
int max = query3.Max(n => n.Length); 
var query4 = query3.TakeWhile(n => n.Length == max); 
+0

最初の単語の長さが同じ単語をすべて選択する方法は? – coco

+0

あなたは最初の単語を 'First()'でチェックすることができます。しかし、その場合は、まずMax()で最大長を求め、その値を使って 'Where 'で単語の残りを取得します。 – Ian

+0

そのコードは何ですか? – coco

2
List<string> orderedWords = words.OrderByDescending(p=>p.Trim().Length).ToList(); 
+0

そして、最初の単語の長さは同じですか? – coco

関連する問題