0

I have a list of objects and need to get the list of records from this list. like I have of Countries and I need to get the list of countries which are in between country with name "Australia" and country "Indonasia", the list will not be sorted.

Am using c#.

I tried to use something like, get the index of first and second and then use that to get the list with a for loop, but would be handy if it can be done in single query.

4

2 回答 2

4

如果您执行以下操作:

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

您将获得所需的结果,而无需遍历列表 3 次。

(这是假设您的国家是例如Country具有Name字符串属性的项目。)

请注意,这根本不会对国家/地区进行排序-从您的问题中不清楚您是否想要这个,但是OrderBySkipWhile.

于 2012-05-28T15:34:58.837 回答
0

这应该做的工作

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