开发者

Returning paged results from WebMethod?

开发者 https://www.devze.com 2023-01-15 22:22 出处:网络
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.

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 Webmetho开发者_开发技巧d?

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号