オブジェクトのリストがあり、このリストからレコードのリストを取得する必要があります。私が持っている国のように、「オーストラリア」という名前の国と「インドネシア」の国の間にある国のリストを取得する必要があるので、リストはソートされません。
c#を使用しています。
最初と2番目のインデックスを取得し、それを使用してforループのあるリストを取得しようとしましたが、単一のクエリで実行できると便利です。
オブジェクトのリストがあり、このリストからレコードのリストを取得する必要があります。私が持っている国のように、「オーストラリア」という名前の国と「インドネシア」の国の間にある国のリストを取得する必要があるので、リストはソートされません。
c#を使用しています。
最初と2番目のインデックスを取得し、それを使用してforループのあるリストを取得しようとしましたが、単一のクエリで実行できると便利です。
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
.
this should do the job
var query = data.SkipWhile(x => x != "Australia").TakeWhile(x => x != "Indonesia")