简体   繁体   中英

Returning paged results from WebMethod?

I need to create WebMethod that will get some data from db and return that to the client.

Now, assume that the amount of data is huge, so I'd like to take and return data in parts.

Is there any way to use yield return in Webmethod?

As I know there is no way to return generic types in WebMethods, but I couldn't use non-generic IEnumerable as well.

How can I accomplish that?

No, you can't yield return from a WebMethod. But you can add two parameters to the method call to allow paged results:

public string[] GetResults(string someQuery)
{
    var results = new List<string>();

    // Fill Results

    return results.ToArray();
}

Becomes:

public string[] GetResults(string someQuery, int pageNum, int pageSize)
{
    var results = new List<string>();

    // Fill Results

    return results.Skip(pageNum * pageSize).Take(pageSize).ToArray();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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