I am using EF Core version 2.2. I have three tables:
I would like to get a list of Books associated with a particular UserId.
I've tried:
var books = await _context.Books
.Include(ub => ub.UserBooks.Where(f => f.UserId == id))
.ToListAsync();
but alas, it doesn't work. I get the error: The Include property lambda expression 'ub => {from UserBook in ub.UserBooks where ([f].UserId == __id_0) select [f]}' is invalid.
How can I get the list of Books associated with a particular UserId? Thank you
You'll need to take the where filter out of the Include - include is used for eager loading, not filtering
A query finding all books which are associated with the given user:
var books = await _context.Books
.Include(b => b.UserBooks)
.Where(b => b.UserBooks.Any(ub => ub.UserId == id))
.ToListAsync();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With