FindAsync with non-primary key value

2020-05-31 05:28发布

问题:

public class Foo
{
     public int Id { get; set; }
     public int UserId { get; set; }
}

This appears to be the way to do this asynchronously:

DatabaseContext db = new DatabaseContext();
Foo foo = await db.Foos.FindAsync(fooid);

How does one asynchronously get all of the Foos for a specific user based on UserId's value?

回答1:

Assuming you are using Entity Framework 6.0 (prerelease):

var userId = ...;
var foos = await db.Foos.Where(x => x.UserId == userId).ToListAsync();