0

オブジェクトのリストがあり、このリストからレコードのリストを取得する必要があります。私が持っている国のように、「オーストラリア」という名前の国と「インドネシア」の国の間にある国のリストを取得する必要があるので、リストはソートされません。

c#を使用しています。

最初と2番目のインデックスを取得し、それを使用してforループのあるリストを取得しようとしましたが、単一のクエリで実行できると便利です。

4

2 に答える 2

4

If you do the following:

var elementsBetween = allElements
        .SkipWhile(c => c.Name != "Australia")
        .Skip(1) // otherwise we'd get Australia too
        .TakeWhile(c => c.Name != "Indonasia");

you'll get the result you want without iterating through the list 3 times.

(This is assuming your countries are e.g. Country items with a Name string property.)

Note that this doesn't sort the countries at all - it's unclear from your question whether you want this or not but it's trivial to add an OrderBy before the SkipWhile.

于 2012-05-28T15:34:58.837 に答える
0

this should do the job

var query = data.SkipWhile(x => x != "Australia").TakeWhile(x => x != "Indonesia")
于 2012-05-28T15:36:13.563 に答える