Lambda表达式选择index> x的所有项目(Lambda expression to select all items where index > x)

我有一个包含int类型的数组的List。 使用lambda表达式如何选择索引大于2的所有项目的列表?

例如,下面的列表应返回8和9:

var items = new List<object>() { new int[3] { 1, 2, 3 }, new int[1] { 4 }, new int[5] { 5, 6, 7, 8, 9 } }; //var overTwoIndexItems = ?

I have a List which contains arrays of type int. Using a lambda expression how can I select a list of all items with an index bigger than 2?

For example the list below should return 8 and 9:

var items = new List<object>() { new int[3] { 1, 2, 3 }, new int[1] { 4 }, new int[5] { 5, 6, 7, 8, 9 } }; //var overTwoIndexItems = ?

最满意答案

您可以使用Skip跳过每个数组中的前三项(即索引为0,1和2的项)。 您可以使用SelectMany来平展结果:

var overTwoIndexItems = items.SelectMany(a => ((int[])a).Skip(3));

或者更安全的版本(当你向对象列表中添加非整数数组的东西时会处理这种情况):

var overTwoIndexItems = items.OfType<int[]>().SelectMany(a => a.Skip(3));

结果:

8, 9

顺便说一句:你为​​什么使用object列表? 这看起来像ArrayList 。 泛型的要点是强类型参数。 请改用List<int[]> 。 然后查询将如下所示:

items.SelectMany(a => a.Skip(3))

You can use Skip to skip first three items in each array (i.e. items with indexes 0, 1 and 2). And you can use SelectMany to flatten results:

var overTwoIndexItems = items.SelectMany(a => ((int[])a).Skip(3));

Or more safe version (which will handle case when you are adding something that is not integer array to your list of objects):

var overTwoIndexItems = items.OfType<int[]>().SelectMany(a => a.Skip(3));

Result:

8, 9

BTW: Why are you using list of object? That looks like ArrayList. Main point of generics is strongly-typed arguments. Use List<int[]> instead. Then query will look like:

items.SelectMany(a => a.Skip(3))Lambda表达式选择index> x的所有项目(Lambda expression to select all items where index > x)

我有一个包含int类型的数组的List。 使用lambda表达式如何选择索引大于2的所有项目的列表?

例如,下面的列表应返回8和9:

var items = new List<object>() { new int[3] { 1, 2, 3 }, new int[1] { 4 }, new int[5] { 5, 6, 7, 8, 9 } }; //var overTwoIndexItems = ?

I have a List which contains arrays of type int. Using a lambda expression how can I select a list of all items with an index bigger than 2?

For example the list below should return 8 and 9:

var items = new List<object>() { new int[3] { 1, 2, 3 }, new int[1] { 4 }, new int[5] { 5, 6, 7, 8, 9 } }; //var overTwoIndexItems = ?

最满意答案

您可以使用Skip跳过每个数组中的前三项(即索引为0,1和2的项)。 您可以使用SelectMany来平展结果:

var overTwoIndexItems = items.SelectMany(a => ((int[])a).Skip(3));

或者更安全的版本(当你向对象列表中添加非整数数组的东西时会处理这种情况):

var overTwoIndexItems = items.OfType<int[]>().SelectMany(a => a.Skip(3));

结果:

8, 9

顺便说一句:你为​​什么使用object列表? 这看起来像ArrayList 。 泛型的要点是强类型参数。 请改用List<int[]> 。 然后查询将如下所示:

items.SelectMany(a => a.Skip(3))

You can use Skip to skip first three items in each array (i.e. items with indexes 0, 1 and 2). And you can use SelectMany to flatten results:

var overTwoIndexItems = items.SelectMany(a => ((int[])a).Skip(3));

Or more safe version (which will handle case when you are adding something that is not integer array to your list of objects):

var overTwoIndexItems = items.OfType<int[]>().SelectMany(a => a.Skip(3));

Result:

8, 9

BTW: Why are you using list of object? That looks like ArrayList. Main point of generics is strongly-typed arguments. Use List<int[]> instead. Then query will look like:

items.SelectMany(a => a.Skip(3))