LINQ with Skip and Take
我使用下面的代码从IEnumerable
获取一些项目,但它始终将源返回为null并计为0并且实际上IEnumerable
存在项目
private void GetItemsPrice(IEnumerable items, int customerNumber) { var a = items.Skip(2).Take(5); }
当我尝试访问它时,它的计数为0
。 这里出了什么问题?
请记住,代码中的变量a
本身就是一个查询 。 它不是查询执行的结果 。 当您使用立即窗口来查看查询时(实际上与延迟执行的查询有关,否则您将有结果而不是查询),它将始终显示
{System.Linq.Enumerable.TakeIterator} count: 0 source: null
您可以使用此代码validation,显然有足够的项目:
int[] items = { 1, 2, 3, 4, 5, 6, 7 }; var a = items.Skip(2).Take(3);
因此,您应该执行查询以查看查询执行的结果 。 写入立即窗口:
a.ToList()
您将看到查询执行的结果:
Count = 3 [0]: 3 [1]: 4 [2]: 5