2

I have this database call in my controller:

var addresses = db.Addresses.Where(a=>a.EmployeeId == id).ToList();

It works fine the way it is, but I am having a hard time to make it async. When I do make it async, I am able to select all records in the table or a single record, just fine. However, I can't seem to get multiple records with matching EmployeeId.

3
  • 1
    "When I do make it async", can you please add the code also? It may be because one employee has multiple Addresses! Commented Apr 4, 2017 at 5:06
  • Yes, some Employees do have multiple addresses. So, how do you write an async code to retrieve a list of addresses with matching EmployeeId? Commented Apr 4, 2017 at 5:49
  • As your code seems correct to me and it should works as expected. Further, you can add Addresses and Employee entity definition, sample tables data and your async call code. Commented Apr 4, 2017 at 6:23

1 Answer 1

2

I found the solution to my own question.

var addresses = db.Addresses.Where(a=>a.EmployeeId == id).ToList();

To make the above call asynchronous I changed it to:

var addresses = await db.Addresses.Where(a=>a.EmployeeId == id).ToListAsync();

The entire method will look like following:

  public async Task<ActionResult> FindAddress(int? id)
    {
        if(id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var addresses = await db.Addresses.Where(a=>a.EmployeeId == id).ToListAsync();

        if (addresses == null)
            return HttpNotFound();

        return View(addresses);

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.