我正在使用ASP.NET Core和Entity Framework。
首先,我选择一个员工,然后选择满足条件的所有员工(为了展示有效的工作):
var a = db.Employee.FirstOrDefault(); var b = db.Employee.Where(x => x.FirstName == "Jack");现在我尝试一样,但异步:
var c = await db.Employee.FirstOrDefaultAsync(); var d = await db.Employee.Where(x => x.FirstName == "Jack");但是,对于“WHERE”,没有异步版本,第二行代码无法编译 - 我收到错误
...不包含GetAwaiter的定义...
在这种情况下,如何在WHERE条件下执行SELECT ?
好的,从答案我看到ToListAsync()将解析“var d = ...”行。 但是,这个问题仍然存在,我之前并不知道它很重要。 在这种情况下,我只是试图选择一组将被删除的记录,对于在代码中进一步处理数据的目的,我不感兴趣访问数据。 所以我修改了所有4个代码版本,目的是同步或异步删除一个或多个记录。 为什么只有最后一个需要ToListAsync(),实际上不会从数据库中检索记录?
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();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();最满意答案
你可以这样做。
如果您需要检索一个对象,则:
var d = await db.Employee.FirstOrDefaultAsync(x => x.FirstName == "Jack");如果您需要检索列表,则:
var d = await db.Employee.Where(x => x.FirstName == "Jack").ToListAsync();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();Entify框架 - 异步选择与哪里条件(Entify Framework - async select with where condition)我正在使用ASP.NET Core和Entity Framework。
首先,我选择一个员工,然后选择满足条件的所有员工(为了展示有效的工作):
var a = db.Employee.FirstOrDefault(); var b = db.Employee.Where(x => x.FirstName == "Jack");现在我尝试一样,但异步:
var c = await db.Employee.FirstOrDefaultAsync(); var d = await db.Employee.Where(x => x.FirstName == "Jack");但是,对于“WHERE”,没有异步版本,第二行代码无法编译 - 我收到错误
...不包含GetAwaiter的定义...
在这种情况下,如何在WHERE条件下执行SELECT ?
好的,从答案我看到ToListAsync()将解析“var d = ...”行。 但是,这个问题仍然存在,我之前并不知道它很重要。 在这种情况下,我只是试图选择一组将被删除的记录,对于在代码中进一步处理数据的目的,我不感兴趣访问数据。 所以我修改了所有4个代码版本,目的是同步或异步删除一个或多个记录。 为什么只有最后一个需要ToListAsync(),实际上不会从数据库中检索记录?
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();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();最满意答案
你可以这样做。
如果您需要检索一个对象,则:
var d = await db.Employee.FirstOrDefaultAsync(x => x.FirstName == "Jack");如果您需要检索列表,则:
var d = await db.Employee.Where(x => x.FirstName == "Jack").ToListAsync();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();
发布评论