3 回答

TA貢獻1831條經(jīng)驗 獲得超9個贊
聽起來您需要使用對象的“跳過”和“獲取”方法。例:
users.Skip(1000).Take(1000)
這將跳過前1000個,并采用下一個1000。您只需要增加每次通話跳過的數(shù)量
您可以將整數(shù)變量與“跳過”參數(shù)一起使用,并且可以調整要跳過的量。然后可以在方法中調用它。
public IEnumerable<user> GetBatch(int pageNumber)
{
return users.Skip(pageNumber * 1000).Take(1000);
}

TA貢獻1890條經(jīng)驗 獲得超9個贊
最簡單的方法可能就是使用GroupByLINQ中的方法:
var batches = myEnumerable
.Select((x, i) => new { x, i })
.GroupBy(p => (p.i / 1000), (p, i) => p.x);
但是,對于更復雜的解決方案,請參閱此博客文章,以了解如何創(chuàng)建自己的擴展方法來執(zhí)行此操作。為后代在此復制:
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> collection, int batchSize)
{
List<T> nextbatch = new List<T>(batchSize);
foreach (T item in collection)
{
nextbatch.Add(item);
if (nextbatch.Count == batchSize)
{
yield return nextbatch;
nextbatch = new List<T>();
// or nextbatch.Clear(); but see Servy's comment below
}
}
if (nextbatch.Count > 0)
yield return nextbatch;
}
- 3 回答
- 0 關注
- 1154 瀏覽
添加回答
舉報