Pingpong
Pingpong

Reputation: 8019

IQueryable/IEnumerable applied usage: database query request against database with Azure CosmosDB

The code below is a query against Cosomos DB, I want to know how many times of requests will be sent to database?

Any improvement on this code in terms of reducing database query?

public async Task<List<Employee>> GetEmployee(string userName)
{
    IEnumerable<User> users = GetByUser(userName);

    return GetEmployee(users);
}

private IEnumerable<User> GetByUser(string userName)
{
    var captureResultHistoryLink = UriFactory.CreateDocumentCollectionUri(Database, Container);

    var user = _documentClient.CreateDocumentQuery<User>(captureResultHistoryLink)
        .Where(r => r.UserName == userName);

    return user;
}


private async Task<List<Employee>> GetEmployee(IEnumerable<User> users)
{
    var selectedUsers = users.Where(o => o.Age > 18);

    foreach (var user in users)
    {
        if (selectedUsers.Contains(user.Id))
        {
            // ...
        }
    }
}

Upvotes: 0

Views: 89

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222722

You could check the number of times of Requests using Fiddler.

However, as it looks like there will be N number of requests based on the users. There are two ways to handle it,

(i) Get all Items using Pagination and filter the users with the Name and Age on the Client Side.

(ii)Change the request to filter the user from the Age and Name from the first Request itself.

Upvotes: 1

Related Questions