[英]Using where clause with continuation in oData client using C#
我正在使用 Microsoft oData 客戶端從第三方 API 檢索用戶信息。 我有以下代碼。
var query = Context.Users.Where(x => x.username.EndsWith("10"));
var result = query.ToList().Select(x => x.username);
但是,這只會返回 100 條記錄。 我可以使用以下代碼來檢索沒有條件的所有記錄;
DataServiceCollection<User> users = new DataServiceCollection<User>(
Context.Users
);
while (users.Continuation != null)
{
//use the token to query for more users
//and load the results back into the collection
users.Load(
Context.Execute<User>(users.Continuation)
);
//print the current count of users retrieved
Console.WriteLine(users.Count);
}
我怎樣才能將兩者結合起來? 即檢索所有帶有條件的記錄(x.username.EndsWith("10"))。
我會說一次只獲取 100 行是一個好習慣。 但是您可以使用 oDatas $top 來獲取超過 100 行。 Microsoft oData 客戶端為此使用 Take function: https://docs.microsoft.com/en-us/odata/client/query-options
我應該能夠簡單地將兩者結合起來:
var result= Context.Users.Where(x => x.username.EndsWith("10")).Take(1000).Select(x => x.username);
我在 oData Client 版本 7.6.2 中使用這樣的查詢,但我認為它也可以在較新的版本中使用。
var users = new List<User>();
DataServiceQueryContinuation<User> nextLink = null;
var query = Context.Users.Where(x => x.username.EndsWith("10")) as DataServiceQuery<User>;
var response = await query.ExecuteAsync() as QueryOperationResponse<User>;
do
{
if (nextLink != null)
{
response = await Context.ExecuteAsync<User>(nextLink) as QueryOperationResponse<User>;
}
users.AddRange(response);
}
while ((nextLink = response.GetContinuation()) != null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.