❔ Why .Where() doesn't work with .Include()?
I need to fetch chats that have current user as a participant, and want to fetch only active users.
I execute the following query:
var query = _db.Chats
.Include(c => c.Participants.Where(cu=>cu.IsActive))
.Include(c=>c.LastMessage)
.ThenInclude(m => m.Sender)
.ThenInclude(cu => cu.User)
.Where(c =>
c.Participants.Any(cu => cu.UserId == _currentUser.MessengerUserId))
.OrderByDescending(c => c.LastMessage != null ? c.LastMessage.Timestamp : c.Created)
.AsNoTracking();
but participants filtering part doesn't work - all the users are included, while it works as intended with 'select only chats with current user as participant' condition.
https://i.stack.imgur.com/WDA6h.png - Code with Resharper Annotations
https://pastebin.com/Bvsxju91 - Generated PgSql script
https://stackoverflow.com/questions/76157093/why-where-doesnt-work-with-include - SO questionPastebin
sql - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Stack Overflow
Why .Where() doesn't work with .Include()?
I need to fetch chats that have current user as a participant, and want to fetch only active users.
I execute the following query:
var query = _db.Chats
.Include(c => c.Participants....
4 Replies
Why do you Include the IsActive of a participant?
did u try my suggestion?
Solved. The problem was … MAPPER
It’s ProjectToType method on Queryable issued some Selects and in EF Select() shadows Include() entirely
After removing ProjectToType everything started working just fine
By the way, 75% time spent on debugging is fixing mapper issues. Think twice if you need it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.