45

I'm using ASP.NET Core with Entity Framework.

First I select an employee, and then all employees that satisfy a condition (for the purpose of displaying what works):

var a = db.Employee.FirstOrDefault();
var b = db.Employee.Where(x => x.FirstName == "Jack");

Now I try the same, but asynchronously:

var c = await db.Employee.FirstOrDefaultAsync();
var d = await db.Employee.Where(x => x.FirstName == "Jack");

However, for the "WHERE" there's no async version, and the second line of code doesn't compile - I get an error

... does not contain a definition for GetAwaiter ...

How do I perform a SELECT with a WHERE condition in this case?


OK, from the answers I see that ToListAsync() will resolve the "var d = ..." line. However, there's a continuation to this issue, I wasn't aware before that it matters. In this case I'm just trying to select a set of records that will be deleted, I'm not interested in accessing the data for the purpose of manipulating it further in the code. So I amended all 4 code versions with purpose to delete one or more records, synchronously or asynchronously. Why does only the last one need a ToListAsync(), won't that actually retrieve the records from the database?

var a = db.Employee.FirstOrDefault();
db.Employee.Remove(a);
// db.Employee.RemoveRange(a); <- this also works?
db.SaveChanges();

var b = db.Employee.Where(x => x.FirstName == "Jack");
db.Employee.RemoveRange(b);
db.SaveChanges();

var c = await db.Employee.FirstOrDefaultAsync();
db.Employee.Remove(c);
await db.SaveChangesAsync();

var d = await db.Employee.Where(x => x.FirstName == "Jack").ToListAsync();
db.Employee.RemoveRange(d);
await db.SaveChangesAsync();
4
  • 1
    Use ToArrayAsync, ToListAsync. Commented Nov 19, 2016 at 21:32
  • RemoveRange will first pull all items from database, then mark as deleted. So there is no harm to call ToListAsync first - otherwise it will be done (synchronously) by RemoveRange itself. Commented Nov 20, 2016 at 8:58
  • But there's no point in pulling all the records from database just to delete them... Commented Nov 20, 2016 at 9:51
  • I agree but that is what you do, and that is how EF works with deletes. There are workarounds though, you can google for them (example: github.com/loresoft/EntityFramework.Extended/blob/master/…) Commented Nov 20, 2016 at 9:56

1 Answer 1

94

You can do it like this.

If you need to retrieve one object then :

var d = await db.Employee.FirstOrDefaultAsync(x => x.FirstName == "Jack");

If you need to retrieve list then :

 var d = await db.Employee.Where(x => x.FirstName == "Jack").ToListAsync();
Sign up to request clarification or add additional context in comments.

2 Comments

How to convert the list into an object to return as a response?
@variable not sure what you are using and this is probably too late, but you could try returning the list as a response

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.