具有非主键值的FindAsync

Rob*_*ert 20 c# async-await asp.net-web-api entity-framework-6

public class Foo
{
     public int Id { get; set; }
     public int UserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这似乎是异步执行此操作的方法:

DatabaseContext db = new DatabaseContext();
Foo foo = await db.Foos.FindAsync(fooid);
Run Code Online (Sandbox Code Playgroud)

如何根据UserId的值异步获取特定用户的所有Foos?

Ste*_*ary 35

假设您正在使用Entity Framework 6.0(预发行版):

var userId = ...;
var foos = await db.Foos.Where(x => x.UserId == userId).ToListAsync();
Run Code Online (Sandbox Code Playgroud)

  • 提示:如果你缺少ToListAsync(),那么使用System.Data.Entity添加; (6认同)