简体   繁体   English

C#中的自动分页功能

[英]automatic paging functionality in C#

I am confused with a flow. 我对流程感到困惑。 I have a list of student objects. 我有一个学生对象列表。

List<Student> StudentLists;

Intially i will be having 10 student objects in the list. 最初我将在列表中有10个学生对象。

There is a button in C# 4.0 winform and when i clicks on the button, C#4.0 winform中有一个按钮,当我点击按钮时,

i need to take first 3 student objects from the list and call a wcf service and send these three student object to the wcf service. 我需要从列表中取出前3个学生对象并调用wcf服务并将这三个学生对象发送到wcf服务。

I have implemented the wcf call back functionality. 我已经实现了wcf回调功能。

After processing the web service, i will get the call back result for theose 3 student objects. 处理完Web服务后,我将获得3个学生对象的回调结果。

Each call back may come in different times. 每次回电可能会在不同的时间进行。

Once i got all the three call back results from webservice, i want to take next 3 available student object and do the same web service call... 一旦我从webservice获得了所有三个回调结果,我想接下来3个可用的学生对象并进行相同的Web服务调用...

I want to do it untill it processes all the 10 items from the lists. 我想这样做,直到它处理列表中的所有10个项目。

But I know how to take each time 3 objects from the lists. 但我知道每次从列表中取出3个对象。 It is like paging. 这就像分页。

var students = StudentLists.Skip(skip).Take(3).ToArray();

But I am doing it in each time button click. 但我每次按下按钮都会这样做。 In each button click, i will take next 3 objects. 在每个按钮单击中,我将接下来3个对象。

Is there any way that to do all these steps without doing button click ? 有没有办法在不点击按钮的情况下完成所有这些步骤?

Seems like you want to process the students in batches, if this is the case you could write an extension method to do this: 好像你想要批量处理学生,如果是这种情况你可以写一个扩展方法来做到这一点:

public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items, int batchSize)
{
    return items.Select((item, inx) => new { item, inx })
                .GroupBy(x => x.inx / batchSize)
                .Select(g => g.Select(x => x.item));
}

Usage: 用法:

foreach (var batch in StudentLists.Batch(3))
{
      //Do something with batch
      //Process
      //Get result etc...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM