Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - async select with where condition

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(); 
like image 676
Marko Avatar asked Nov 19 '16 21:11

Marko


People also ask

Why does where () not have an asynchronous counterpart?

Note that there are no async versions of some LINQ operators such as Where or OrderBy, because these only build up the LINQ expression tree and don't cause the query to be executed in the database. Only operators which cause query execution have async counterparts.

What does SaveChangesAsync return?

SaveChangesAsync() returns the number of lines changed. It is possible (at least theoretically) that two user will simultenously work to delete the same id in the database.

Should I use async EF core?

Generally speaking, if there are asynchronous APIs, then you should use them for new code. Asynchronous code frees up the calling thread. If your application is a GUI application, this can free up the UI thread; if your application is a server application, this can free up threads to handle other requests.

Can IQueryable be async?

ToListAsync(IQueryable)Creates a List<T> from an IQueryable by enumerating it asynchronously.


1 Answers

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(); 
like image 105
Sampath Avatar answered Oct 05 '22 13:10

Sampath