C
C#2y ago
bookuha

❔ 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 question
Pastebin
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
Monsieur Wholesome
Why do you Include the IsActive of a participant?
Henkypenky
Henkypenky2y ago
did u try my suggestion?
.selectMany(c => c.Participants)
.Where(cu => cu.UserId == _currentUser.MessengerUserId && cu.IsActive is true)
.selectMany(c => c.Participants)
.Where(cu => cu.UserId == _currentUser.MessengerUserId && cu.IsActive is true)
bookuha
bookuhaOP2y ago
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
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server